Compare commits
15 Commits
Author | SHA1 | Date | |
---|---|---|---|
50d7ecf76d | |||
42a2a80b87 | |||
54deded929 | |||
39bdf6d41e | |||
074190e03c | |||
256514c7c9 | |||
556be08c4e | |||
1cbca5eecb | |||
95017b8c66 | |||
4a892fbdc9 | |||
9eb5b7a10d | |||
d64594ec74 | |||
6a1a03566a | |||
13f5294aa3 | |||
9444b4a647 |
11
.github/workflows/build.yml
vendored
11
.github/workflows/build.yml
vendored
@ -46,6 +46,7 @@ jobs:
|
||||
env:
|
||||
POWERSHELL_TELEMETRY_OPTOUT: 1
|
||||
DOTNET_CLI_TELEMETRY_OPTOUT: 1
|
||||
RYUJINX_BASE_VERSION: "1.1.0"
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-dotnet@v1
|
||||
@ -59,24 +60,24 @@ jobs:
|
||||
- name: Clear
|
||||
run: dotnet clean && dotnet nuget locals all --clear
|
||||
- name: Build
|
||||
run: dotnet build -c "${{ matrix.configuration }}" /p:Version="1.1.0" /p:SourceRevisionId="${{ steps.git_short_hash.outputs.result }}" /p:ExtraDefineConstants=DISABLE_UPDATER
|
||||
run: dotnet build -c "${{ matrix.configuration }}" /p:Version="${{ env.RYUJINX_BASE_VERSION }}" /p:SourceRevisionId="${{ steps.git_short_hash.outputs.result }}" /p:ExtraDefineConstants=DISABLE_UPDATER
|
||||
- name: Test
|
||||
run: dotnet test -c "${{ matrix.configuration }}"
|
||||
- name: Publish Ryujinx
|
||||
run: dotnet publish -c "${{ matrix.configuration }}" -r "${{ matrix.DOTNET_RUNTIME_IDENTIFIER }}" -o ./publish /p:Version="1.1.0" /p:DebugType=embedded /p:SourceRevisionId="${{ steps.git_short_hash.outputs.result }}" /p:ExtraDefineConstants=DISABLE_UPDATER Ryujinx --self-contained
|
||||
run: dotnet publish -c "${{ matrix.configuration }}" -r "${{ matrix.DOTNET_RUNTIME_IDENTIFIER }}" -o ./publish /p:Version="${{ env.RYUJINX_BASE_VERSION }}" /p:DebugType=embedded /p:SourceRevisionId="${{ steps.git_short_hash.outputs.result }}" /p:ExtraDefineConstants=DISABLE_UPDATER Ryujinx --self-contained
|
||||
if: github.event_name == 'pull_request'
|
||||
- name: Publish Ryujinx.Headless.SDL2
|
||||
run: dotnet publish -c "${{ matrix.configuration }}" -r "${{ matrix.DOTNET_RUNTIME_IDENTIFIER }}" -o ./publish_sdl2_headless /p:Version="1.1.0" /p:DebugType=embedded /p:SourceRevisionId="${{ steps.git_short_hash.outputs.result }}" /p:ExtraDefineConstants=DISABLE_UPDATER Ryujinx.Headless.SDL2 --self-contained
|
||||
run: dotnet publish -c "${{ matrix.configuration }}" -r "${{ matrix.DOTNET_RUNTIME_IDENTIFIER }}" -o ./publish_sdl2_headless /p:Version="${{ env.RYUJINX_BASE_VERSION }}" /p:DebugType=embedded /p:SourceRevisionId="${{ steps.git_short_hash.outputs.result }}" /p:ExtraDefineConstants=DISABLE_UPDATER Ryujinx.Headless.SDL2 --self-contained
|
||||
if: github.event_name == 'pull_request'
|
||||
- name: Upload Ryujinx artifact
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: ryujinx-${{ matrix.configuration }}-1.0.0+${{ steps.git_short_hash.outputs.result }}-${{ matrix.RELEASE_ZIP_OS_NAME }}
|
||||
name: ryujinx-${{ matrix.configuration }}-${{ env.RYUJINX_BASE_VERSION }}+${{ steps.git_short_hash.outputs.result }}-${{ matrix.RELEASE_ZIP_OS_NAME }}
|
||||
path: publish
|
||||
if: github.event_name == 'pull_request'
|
||||
- name: Upload Ryujinx.Headless.SDL2 artifact
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: ryujinx-headless-sdl2-${{ matrix.configuration }}-1.0.0+${{ steps.git_short_hash.outputs.result }}-${{ matrix.RELEASE_ZIP_OS_NAME }}
|
||||
name: ryujinx-headless-sdl2-${{ matrix.configuration }}-${{ env.RYUJINX_BASE_VERSION }}+${{ steps.git_short_hash.outputs.result }}-${{ matrix.RELEASE_ZIP_OS_NAME }}
|
||||
path: publish_sdl2_headless
|
||||
if: github.event_name == 'pull_request'
|
||||
|
25
ARMeilleure/Decoders/OpCodeT32MemImm12.cs
Normal file
25
ARMeilleure/Decoders/OpCodeT32MemImm12.cs
Normal file
@ -0,0 +1,25 @@
|
||||
namespace ARMeilleure.Decoders
|
||||
{
|
||||
class OpCodeT32MemImm12 : OpCodeT32, IOpCode32Mem
|
||||
{
|
||||
public int Rt { get; }
|
||||
public int Rn { get; }
|
||||
public bool WBack => false;
|
||||
public bool IsLoad { get; }
|
||||
public bool Index => true;
|
||||
public bool Add => true;
|
||||
public int Immediate { get; }
|
||||
|
||||
public new static OpCode Create(InstDescriptor inst, ulong address, int opCode) => new OpCodeT32MemImm12(inst, address, opCode);
|
||||
|
||||
public OpCodeT32MemImm12(InstDescriptor inst, ulong address, int opCode) : base(inst, address, opCode)
|
||||
{
|
||||
Rt = (opCode >> 12) & 0xf;
|
||||
Rn = (opCode >> 16) & 0xf;
|
||||
|
||||
Immediate = opCode & 0xfff;
|
||||
|
||||
IsLoad = ((opCode >> 20) & 1) != 0;
|
||||
}
|
||||
}
|
||||
}
|
29
ARMeilleure/Decoders/OpCodeT32MemImm8.cs
Normal file
29
ARMeilleure/Decoders/OpCodeT32MemImm8.cs
Normal file
@ -0,0 +1,29 @@
|
||||
namespace ARMeilleure.Decoders
|
||||
{
|
||||
class OpCodeT32MemImm8 : OpCodeT32, IOpCode32Mem
|
||||
{
|
||||
public int Rt { get; }
|
||||
public int Rn { get; }
|
||||
public bool WBack { get; }
|
||||
public bool IsLoad { get; }
|
||||
public bool Index { get; }
|
||||
public bool Add { get; }
|
||||
public int Immediate { get; }
|
||||
|
||||
public new static OpCode Create(InstDescriptor inst, ulong address, int opCode) => new OpCodeT32MemImm8(inst, address, opCode);
|
||||
|
||||
public OpCodeT32MemImm8(InstDescriptor inst, ulong address, int opCode) : base(inst, address, opCode)
|
||||
{
|
||||
Rt = (opCode >> 12) & 0xf;
|
||||
Rn = (opCode >> 16) & 0xf;
|
||||
|
||||
Index = ((opCode >> 10) & 1) != 0;
|
||||
Add = ((opCode >> 9) & 1) != 0;
|
||||
WBack = ((opCode >> 8) & 1) != 0;
|
||||
|
||||
Immediate = opCode & 0xff;
|
||||
|
||||
IsLoad = ((opCode >> 20) & 1) != 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -1065,6 +1065,26 @@ namespace ARMeilleure.Decoders
|
||||
SetT32("11110x011011xxxx0xxx1111xxxxxxxx", InstName.Cmp, InstEmit32.Cmp, OpCodeT32AluImm.Create);
|
||||
SetT32("11101010100<xxxx0xxx<<<<xxxxxxxx", InstName.Eor, InstEmit32.Eor, OpCodeT32AluRsImm.Create);
|
||||
SetT32("11110x00100<xxxx0xxx<<<<xxxxxxxx", InstName.Eor, InstEmit32.Eor, OpCodeT32AluImm.Create);
|
||||
SetT32("111110000101xxxx<<<<10x1xxxxxxxx", InstName.Ldr, InstEmit32.Ldr, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110000101xxxx<<<<1100xxxxxxxx", InstName.Ldr, InstEmit32.Ldr, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110000101xxxx<<<<11x1xxxxxxxx", InstName.Ldr, InstEmit32.Ldr, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110001101xxxxxxxxxxxxxxxxxxxx", InstName.Ldr, InstEmit32.Ldr, OpCodeT32MemImm12.Create);
|
||||
SetT32("111110000001xxxx<<<<10x1xxxxxxxx", InstName.Ldrb, InstEmit32.Ldrb, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110000001xxxx<<<<1100xxxxxxxx", InstName.Ldrb, InstEmit32.Ldrb, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110000001xxxx<<<<11x1xxxxxxxx", InstName.Ldrb, InstEmit32.Ldrb, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110001001xxxxxxxxxxxxxxxxxxxx", InstName.Ldrb, InstEmit32.Ldrb, OpCodeT32MemImm12.Create);
|
||||
SetT32("111110000011xxxx<<<<10x1xxxxxxxx", InstName.Ldrh, InstEmit32.Ldrh, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110000011xxxx<<<<1100xxxxxxxx", InstName.Ldrh, InstEmit32.Ldrh, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110000011xxxx<<<<11x1xxxxxxxx", InstName.Ldrh, InstEmit32.Ldrh, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110001011xxxxxxxxxxxxxxxxxxxx", InstName.Ldrh, InstEmit32.Ldrh, OpCodeT32MemImm12.Create);
|
||||
SetT32("111110010001xxxx<<<<10x1xxxxxxxx", InstName.Ldrsb, InstEmit32.Ldrsb, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110010001xxxx<<<<1100xxxxxxxx", InstName.Ldrsb, InstEmit32.Ldrsb, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110010001xxxx<<<<11x1xxxxxxxx", InstName.Ldrsb, InstEmit32.Ldrsb, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110011001xxxxxxxxxxxxxxxxxxxx", InstName.Ldrsb, InstEmit32.Ldrsb, OpCodeT32MemImm12.Create);
|
||||
SetT32("111110010011xxxx<<<<10x1xxxxxxxx", InstName.Ldrsh, InstEmit32.Ldrsh, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110010011xxxx<<<<1100xxxxxxxx", InstName.Ldrsh, InstEmit32.Ldrsh, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110010011xxxx<<<<11x1xxxxxxxx", InstName.Ldrsh, InstEmit32.Ldrsh, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110011011xxxxxxxxxxxxxxxxxxxx", InstName.Ldrsh, InstEmit32.Ldrsh, OpCodeT32MemImm12.Create);
|
||||
SetT32("11101010010x11110xxxxxxxxxxxxxxx", InstName.Mov, InstEmit32.Mov, OpCodeT32AluRsImm.Create);
|
||||
SetT32("11110x00010x11110xxxxxxxxxxxxxxx", InstName.Mov, InstEmit32.Mov, OpCodeT32AluImm.Create);
|
||||
SetT32("11101010011x11110xxxxxxxxxxxxxxx", InstName.Mvn, InstEmit32.Mvn, OpCodeT32AluRsImm.Create);
|
||||
@ -1077,6 +1097,12 @@ namespace ARMeilleure.Decoders
|
||||
SetT32("11110x01110xxxxx0xxxxxxxxxxxxxxx", InstName.Rsb, InstEmit32.Rsb, OpCodeT32AluImm.Create);
|
||||
SetT32("11101011011xxxxx0xxxxxxxxxxxxxxx", InstName.Sbc, InstEmit32.Sbc, OpCodeT32AluRsImm.Create);
|
||||
SetT32("11110x01011xxxxx0xxxxxxxxxxxxxxx", InstName.Sbc, InstEmit32.Sbc, OpCodeT32AluImm.Create);
|
||||
SetT32("111110000100xxxxxxxx1<<>xxxxxxxx", InstName.Str, InstEmit32.Str, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110001100xxxxxxxxxxxxxxxxxxxx", InstName.Str, InstEmit32.Str, OpCodeT32MemImm12.Create);
|
||||
SetT32("111110000000xxxxxxxx1<<>xxxxxxxx", InstName.Strb, InstEmit32.Strb, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110001000xxxxxxxxxxxxxxxxxxxx", InstName.Strb, InstEmit32.Strb, OpCodeT32MemImm12.Create);
|
||||
SetT32("111110000010xxxxxxxx1<<>xxxxxxxx", InstName.Strh, InstEmit32.Strh, OpCodeT32MemImm8.Create);
|
||||
SetT32("111110001010xxxxxxxxxxxxxxxxxxxx", InstName.Strh, InstEmit32.Strh, OpCodeT32MemImm12.Create);
|
||||
SetT32("11101011101<xxxx0xxx<<<<xxxxxxxx", InstName.Sub, InstEmit32.Sub, OpCodeT32AluRsImm.Create);
|
||||
SetT32("11110x01101<xxxx0xxx<<<<xxxxxxxx", InstName.Sub, InstEmit32.Sub, OpCodeT32AluImm.Create);
|
||||
SetT32("111010101001xxxx0xxx1111xxxxxxxx", InstName.Teq, InstEmit32.Teq, OpCodeT32AluRsImm.Create);
|
||||
|
@ -103,7 +103,7 @@ namespace ARMeilleure.Signal
|
||||
// Unix siginfo struct locations.
|
||||
// NOTE: These are incredibly likely to be different between kernel version and architectures.
|
||||
|
||||
config.StructAddressOffset = 16; // si_addr
|
||||
config.StructAddressOffset = OperatingSystem.IsMacOS() ? 24 : 16; // si_addr
|
||||
config.StructWriteOffset = 8; // si_code
|
||||
|
||||
_signalHandlerPtr = Marshal.GetFunctionPointerForDelegate(GenerateUnixSignalHandler(_handlerConfig));
|
||||
|
@ -21,6 +21,7 @@ namespace ARMeilleure.Signal
|
||||
static class UnixSignalHandlerRegistration
|
||||
{
|
||||
private const int SIGSEGV = 11;
|
||||
private const int SIGBUS = 10;
|
||||
private const int SA_SIGINFO = 0x00000004;
|
||||
|
||||
[DllImport("libc", SetLastError = true)]
|
||||
@ -43,7 +44,17 @@ namespace ARMeilleure.Signal
|
||||
|
||||
if (result != 0)
|
||||
{
|
||||
throw new InvalidOperationException($"Could not register sigaction. Error: {result}");
|
||||
throw new InvalidOperationException($"Could not register SIGSEGV sigaction. Error: {result}");
|
||||
}
|
||||
|
||||
if (OperatingSystem.IsMacOS())
|
||||
{
|
||||
result = sigaction(SIGBUS, ref sig, out SigAction oldb);
|
||||
|
||||
if (result != 0)
|
||||
{
|
||||
throw new InvalidOperationException($"Could not register SIGBUS sigaction. Error: {result}");
|
||||
}
|
||||
}
|
||||
|
||||
return old;
|
||||
@ -51,7 +62,7 @@ namespace ARMeilleure.Signal
|
||||
|
||||
public static bool RestoreExceptionHandler(SigAction oldAction)
|
||||
{
|
||||
return sigaction(SIGSEGV, ref oldAction, out SigAction _) == 0;
|
||||
return sigaction(SIGSEGV, ref oldAction, out SigAction _) == 0 && (!OperatingSystem.IsMacOS() || sigaction(SIGBUS, ref oldAction, out SigAction _) == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ namespace ARMeilleure.Translation
|
||||
/// </summary>
|
||||
/// <typeparam name="K">Key</typeparam>
|
||||
/// <typeparam name="V">Value</typeparam>
|
||||
public class IntervalTree<K, V> where K : IComparable<K>
|
||||
class IntervalTree<K, V> where K : IComparable<K>
|
||||
{
|
||||
private const int ArrayGrowthSize = 32;
|
||||
|
||||
@ -53,7 +53,7 @@ namespace ARMeilleure.Translation
|
||||
/// <returns>Number of intervals found</returns>
|
||||
public int Get(K start, K end, ref K[] overlaps, int overlapCount = 0)
|
||||
{
|
||||
GetValues(_root, start, end, ref overlaps, ref overlapCount);
|
||||
GetKeys(_root, start, end, ref overlaps, ref overlapCount);
|
||||
|
||||
return overlapCount;
|
||||
}
|
||||
@ -180,20 +180,20 @@ namespace ARMeilleure.Translation
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve all values that overlap the given start and end keys.
|
||||
/// Retrieve all keys that overlap the given start and end keys.
|
||||
/// </summary>
|
||||
/// <param name="start">Start of the range</param>
|
||||
/// <param name="end">End of the range</param>
|
||||
/// <param name="overlaps">Overlaps array to place results in</param>
|
||||
/// <param name="overlapCount">Overlaps count to update</param>
|
||||
private void GetValues(IntervalTreeNode<K, V> node, K start, K end, ref K[] overlaps, ref int overlapCount)
|
||||
private void GetKeys(IntervalTreeNode<K, V> node, K start, K end, ref K[] overlaps, ref int overlapCount)
|
||||
{
|
||||
if (node == null || start.CompareTo(node.Max) >= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GetValues(node.Left, start, end, ref overlaps, ref overlapCount);
|
||||
GetKeys(node.Left, start, end, ref overlaps, ref overlapCount);
|
||||
|
||||
bool endsOnRight = end.CompareTo(node.Start) > 0;
|
||||
if (endsOnRight)
|
||||
@ -208,7 +208,7 @@ namespace ARMeilleure.Translation
|
||||
overlaps[overlapCount++] = node.Start;
|
||||
}
|
||||
|
||||
GetValues(node.Right, start, end, ref overlaps, ref overlapCount);
|
||||
GetKeys(node.Right, start, end, ref overlaps, ref overlapCount);
|
||||
}
|
||||
}
|
||||
|
||||
@ -717,40 +717,40 @@ namespace ARMeilleure.Translation
|
||||
/// </summary>
|
||||
/// <typeparam name="K">Key type of the node</typeparam>
|
||||
/// <typeparam name="V">Value type of the node</typeparam>
|
||||
internal class IntervalTreeNode<K, V>
|
||||
class IntervalTreeNode<K, V>
|
||||
{
|
||||
internal bool Color = true;
|
||||
internal IntervalTreeNode<K, V> Left = null;
|
||||
internal IntervalTreeNode<K, V> Right = null;
|
||||
internal IntervalTreeNode<K, V> Parent = null;
|
||||
public bool Color = true;
|
||||
public IntervalTreeNode<K, V> Left = null;
|
||||
public IntervalTreeNode<K, V> Right = null;
|
||||
public IntervalTreeNode<K, V> Parent = null;
|
||||
|
||||
/// <summary>
|
||||
/// The start of the range.
|
||||
/// </summary>
|
||||
internal K Start;
|
||||
public K Start;
|
||||
|
||||
/// <summary>
|
||||
/// The end of the range.
|
||||
/// </summary>
|
||||
internal K End;
|
||||
public K End;
|
||||
|
||||
/// <summary>
|
||||
/// The maximum end value of this node and all its children.
|
||||
/// </summary>
|
||||
internal K Max;
|
||||
public K Max;
|
||||
|
||||
/// <summary>
|
||||
/// Value stored on this node.
|
||||
/// </summary>
|
||||
internal V Value;
|
||||
public V Value;
|
||||
|
||||
public IntervalTreeNode(K start, K end, V value, IntervalTreeNode<K, V> parent)
|
||||
{
|
||||
this.Start = start;
|
||||
this.End = end;
|
||||
this.Max = end;
|
||||
this.Value = value;
|
||||
this.Parent = parent;
|
||||
Start = start;
|
||||
End = end;
|
||||
Max = end;
|
||||
Value = value;
|
||||
Parent = parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
|
||||
namespace Ryujinx.Common.Collections
|
||||
@ -212,7 +210,7 @@ namespace Ryujinx.Common.Collections
|
||||
/// <param name="overlaps">Overlaps array to place results in</param>
|
||||
/// <param name="overlapCount">Overlaps count to update</param>
|
||||
private void GetValues(IntervalTreeNode<K, V> node, K start, K end, ref V[] overlaps, ref int overlapCount)
|
||||
{
|
||||
{
|
||||
if (node == null || start.CompareTo(node.Max) >= 0)
|
||||
{
|
||||
return;
|
||||
@ -624,7 +622,7 @@ namespace Ryujinx.Common.Collections
|
||||
node.Right = LeftOf(right);
|
||||
if (node.Right != null)
|
||||
{
|
||||
node.Right.Parent = node;
|
||||
node.Right.Parent = node;
|
||||
}
|
||||
IntervalTreeNode<K, V> nodeParent = ParentOf(node);
|
||||
right.Parent = nodeParent;
|
||||
@ -638,7 +636,7 @@ namespace Ryujinx.Common.Collections
|
||||
}
|
||||
else
|
||||
{
|
||||
nodeParent.Right = right;
|
||||
nodeParent.Right = right;
|
||||
}
|
||||
right.Left = node;
|
||||
node.Parent = right;
|
||||
@ -779,37 +777,37 @@ namespace Ryujinx.Common.Collections
|
||||
/// </summary>
|
||||
/// <typeparam name="K">Key type of the node</typeparam>
|
||||
/// <typeparam name="V">Value type of the node</typeparam>
|
||||
internal class IntervalTreeNode<K, V>
|
||||
class IntervalTreeNode<K, V>
|
||||
{
|
||||
internal bool Color = true;
|
||||
internal IntervalTreeNode<K, V> Left = null;
|
||||
internal IntervalTreeNode<K, V> Right = null;
|
||||
internal IntervalTreeNode<K, V> Parent = null;
|
||||
public bool Color = true;
|
||||
public IntervalTreeNode<K, V> Left = null;
|
||||
public IntervalTreeNode<K, V> Right = null;
|
||||
public IntervalTreeNode<K, V> Parent = null;
|
||||
|
||||
/// <summary>
|
||||
/// The start of the range.
|
||||
/// </summary>
|
||||
internal K Start;
|
||||
public K Start;
|
||||
|
||||
/// <summary>
|
||||
/// The end of the range - maximum of all in the Values list.
|
||||
/// </summary>
|
||||
internal K End;
|
||||
public K End;
|
||||
|
||||
/// <summary>
|
||||
/// The maximum end value of this node and all its children.
|
||||
/// </summary>
|
||||
internal K Max;
|
||||
public K Max;
|
||||
|
||||
internal List<RangeNode<K, V>> Values;
|
||||
public List<RangeNode<K, V>> Values;
|
||||
|
||||
public IntervalTreeNode(K start, K end, V value, IntervalTreeNode<K, V> parent)
|
||||
{
|
||||
this.Start = start;
|
||||
this.End = end;
|
||||
this.Max = end;
|
||||
this.Values = new List<RangeNode<K, V>> { new RangeNode<K, V>(start, end, value) };
|
||||
this.Parent = parent;
|
||||
Start = start;
|
||||
End = end;
|
||||
Max = end;
|
||||
Values = new List<RangeNode<K, V>> { new RangeNode<K, V>(start, end, value) };
|
||||
Parent = parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -921,10 +921,10 @@ namespace Ryujinx.Common.Collections
|
||||
|
||||
public bool IsReadOnly => false;
|
||||
|
||||
public V this[K key]
|
||||
{
|
||||
public V this[K key]
|
||||
{
|
||||
get => Get(key);
|
||||
set => Add(key, value);
|
||||
set => Add(key, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -967,20 +967,20 @@ namespace Ryujinx.Common.Collections
|
||||
/// </summary>
|
||||
/// <typeparam name="K">Key of the node</typeparam>
|
||||
/// <typeparam name="V">Value of the node</typeparam>
|
||||
internal class Node<K, V>
|
||||
class Node<K, V>
|
||||
{
|
||||
internal bool Color = true;
|
||||
internal Node<K, V> Left = null;
|
||||
internal Node<K, V> Right = null;
|
||||
internal Node<K, V> Parent = null;
|
||||
internal K Key;
|
||||
internal V Value;
|
||||
public bool Color = true;
|
||||
public Node<K, V> Left = null;
|
||||
public Node<K, V> Right = null;
|
||||
public Node<K, V> Parent = null;
|
||||
public K Key;
|
||||
public V Value;
|
||||
|
||||
public Node(K key, V value, Node<K, V> parent)
|
||||
{
|
||||
this.Key = key;
|
||||
this.Value = value;
|
||||
this.Parent = parent;
|
||||
Key = key;
|
||||
Value = value;
|
||||
Parent = parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ namespace Ryujinx.Cpu
|
||||
_baseAddress = (ulong)_addressSpace.Pointer;
|
||||
ulong endAddress = _baseAddress + addressSpace.Size;
|
||||
|
||||
_trackingEvent = new TrackingEventDelegate(tracking.VirtualMemoryEvent);
|
||||
_trackingEvent = new TrackingEventDelegate(tracking.VirtualMemoryEventEh);
|
||||
bool added = NativeSignalHandler.AddTrackedRegion((nuint)_baseAddress, (nuint)endAddress, Marshal.GetFunctionPointerForDelegate(_trackingEvent));
|
||||
|
||||
if (!added)
|
||||
|
@ -25,6 +25,7 @@ namespace Ryujinx.Cpu
|
||||
|
||||
private const int PointerTagBit = 62;
|
||||
|
||||
private readonly MemoryBlock _backingMemory;
|
||||
private readonly InvalidAccessHandler _invalidAccessHandler;
|
||||
|
||||
/// <summary>
|
||||
@ -50,10 +51,12 @@ namespace Ryujinx.Cpu
|
||||
/// <summary>
|
||||
/// Creates a new instance of the memory manager.
|
||||
/// </summary>
|
||||
/// <param name="backingMemory">Physical backing memory where virtual memory will be mapped to</param>
|
||||
/// <param name="addressSpaceSize">Size of the address space</param>
|
||||
/// <param name="invalidAccessHandler">Optional function to handle invalid memory accesses</param>
|
||||
public MemoryManager(ulong addressSpaceSize, InvalidAccessHandler invalidAccessHandler = null)
|
||||
public MemoryManager(MemoryBlock backingMemory, ulong addressSpaceSize, InvalidAccessHandler invalidAccessHandler = null)
|
||||
{
|
||||
_backingMemory = backingMemory;
|
||||
_invalidAccessHandler = invalidAccessHandler;
|
||||
|
||||
ulong asSize = PageSize;
|
||||
@ -73,18 +76,19 @@ namespace Ryujinx.Cpu
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Map(ulong va, nuint hostAddress, ulong size)
|
||||
public void Map(ulong va, ulong pa, ulong size)
|
||||
{
|
||||
AssertValidAddressAndSize(va, size);
|
||||
|
||||
ulong remainingSize = size;
|
||||
ulong oVa = va;
|
||||
ulong oPa = pa;
|
||||
while (remainingSize != 0)
|
||||
{
|
||||
_pageTable.Write((va / PageSize) * PteSize, hostAddress);
|
||||
_pageTable.Write((va / PageSize) * PteSize, PaToPte(pa));
|
||||
|
||||
va += PageSize;
|
||||
hostAddress += PageSize;
|
||||
pa += PageSize;
|
||||
remainingSize -= PageSize;
|
||||
}
|
||||
Tracking.Map(oVa, size);
|
||||
@ -107,7 +111,7 @@ namespace Ryujinx.Cpu
|
||||
ulong remainingSize = size;
|
||||
while (remainingSize != 0)
|
||||
{
|
||||
_pageTable.Write((va / PageSize) * PteSize, (nuint)0);
|
||||
_pageTable.Write((va / PageSize) * PteSize, 0UL);
|
||||
|
||||
va += PageSize;
|
||||
remainingSize -= PageSize;
|
||||
@ -123,8 +127,21 @@ namespace Ryujinx.Cpu
|
||||
/// <inheritdoc/>
|
||||
public T ReadTracked<T>(ulong va) where T : unmanaged
|
||||
{
|
||||
SignalMemoryTracking(va, (ulong)Unsafe.SizeOf<T>(), false);
|
||||
return MemoryMarshal.Cast<byte, T>(GetSpan(va, Unsafe.SizeOf<T>()))[0];
|
||||
try
|
||||
{
|
||||
SignalMemoryTracking(va, (ulong)Unsafe.SizeOf<T>(), false);
|
||||
|
||||
return Read<T>(va);
|
||||
}
|
||||
catch (InvalidMemoryRegionException)
|
||||
{
|
||||
if (_invalidAccessHandler == null || !_invalidAccessHandler(va))
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@ -177,7 +194,7 @@ namespace Ryujinx.Cpu
|
||||
|
||||
if (IsContiguousAndMapped(va, data.Length))
|
||||
{
|
||||
data.CopyTo(GetHostSpanContiguous(va, data.Length));
|
||||
data.CopyTo(_backingMemory.GetSpan(GetPhysicalAddressInternal(va), data.Length));
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -185,18 +202,22 @@ namespace Ryujinx.Cpu
|
||||
|
||||
if ((va & PageMask) != 0)
|
||||
{
|
||||
ulong pa = GetPhysicalAddressInternal(va);
|
||||
|
||||
size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
|
||||
|
||||
data.Slice(0, size).CopyTo(GetHostSpanContiguous(va, size));
|
||||
data.Slice(0, size).CopyTo(_backingMemory.GetSpan(pa, size));
|
||||
|
||||
offset += size;
|
||||
}
|
||||
|
||||
for (; offset < data.Length; offset += size)
|
||||
{
|
||||
ulong pa = GetPhysicalAddressInternal(va + (ulong)offset);
|
||||
|
||||
size = Math.Min(data.Length - offset, PageSize);
|
||||
|
||||
data.Slice(offset, size).CopyTo(GetHostSpanContiguous(va + (ulong)offset, size));
|
||||
data.Slice(offset, size).CopyTo(_backingMemory.GetSpan(pa, size));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -224,7 +245,7 @@ namespace Ryujinx.Cpu
|
||||
|
||||
if (IsContiguousAndMapped(va, size))
|
||||
{
|
||||
return GetHostSpanContiguous(va, size);
|
||||
return _backingMemory.GetSpan(GetPhysicalAddressInternal(va), size);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -251,7 +272,7 @@ namespace Ryujinx.Cpu
|
||||
SignalMemoryTracking(va, (ulong)size, true);
|
||||
}
|
||||
|
||||
return new WritableRegion(null, va, new NativeMemoryManager<byte>((byte*)GetHostAddress(va), size).Memory);
|
||||
return new WritableRegion(null, va, _backingMemory.GetMemory(GetPhysicalAddressInternal(va), size));
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -264,7 +285,7 @@ namespace Ryujinx.Cpu
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public unsafe ref T GetRef<T>(ulong va) where T : unmanaged
|
||||
public ref T GetRef<T>(ulong va) where T : unmanaged
|
||||
{
|
||||
if (!IsContiguous(va, Unsafe.SizeOf<T>()))
|
||||
{
|
||||
@ -273,7 +294,7 @@ namespace Ryujinx.Cpu
|
||||
|
||||
SignalMemoryTracking(va, (ulong)Unsafe.SizeOf<T>(), true);
|
||||
|
||||
return ref *(T*)GetHostAddress(va);
|
||||
return ref _backingMemory.GetRef<T>(GetPhysicalAddressInternal(va));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -293,7 +314,7 @@ namespace Ryujinx.Cpu
|
||||
return (int)(vaSpan / PageSize);
|
||||
}
|
||||
|
||||
private void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException();
|
||||
private static void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException();
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private bool IsContiguousAndMapped(ulong va, int size) => IsContiguous(va, size) && IsMapped(va);
|
||||
@ -315,7 +336,7 @@ namespace Ryujinx.Cpu
|
||||
return false;
|
||||
}
|
||||
|
||||
if (GetHostAddress(va) + PageSize != GetHostAddress(va + PageSize))
|
||||
if (GetPhysicalAddressInternal(va) + PageSize != GetPhysicalAddressInternal(va + PageSize))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -327,11 +348,11 @@ namespace Ryujinx.Cpu
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<HostMemoryRange> GetPhysicalRegions(ulong va, ulong size)
|
||||
public IEnumerable<MemoryRange> GetPhysicalRegions(ulong va, ulong size)
|
||||
{
|
||||
if (size == 0)
|
||||
{
|
||||
return Enumerable.Empty<HostMemoryRange>();
|
||||
return Enumerable.Empty<MemoryRange>();
|
||||
}
|
||||
|
||||
if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size))
|
||||
@ -341,9 +362,9 @@ namespace Ryujinx.Cpu
|
||||
|
||||
int pages = GetPagesCount(va, (uint)size, out va);
|
||||
|
||||
var regions = new List<HostMemoryRange>();
|
||||
var regions = new List<MemoryRange>();
|
||||
|
||||
nuint regionStart = GetHostAddress(va);
|
||||
ulong regionStart = GetPhysicalAddressInternal(va);
|
||||
ulong regionSize = PageSize;
|
||||
|
||||
for (int page = 0; page < pages - 1; page++)
|
||||
@ -353,12 +374,12 @@ namespace Ryujinx.Cpu
|
||||
return null;
|
||||
}
|
||||
|
||||
nuint newHostAddress = GetHostAddress(va + PageSize);
|
||||
ulong newPa = GetPhysicalAddressInternal(va + PageSize);
|
||||
|
||||
if (GetHostAddress(va) + PageSize != newHostAddress)
|
||||
if (GetPhysicalAddressInternal(va) + PageSize != newPa)
|
||||
{
|
||||
regions.Add(new HostMemoryRange(regionStart, regionSize));
|
||||
regionStart = newHostAddress;
|
||||
regions.Add(new MemoryRange(regionStart, regionSize));
|
||||
regionStart = newPa;
|
||||
regionSize = 0;
|
||||
}
|
||||
|
||||
@ -366,7 +387,7 @@ namespace Ryujinx.Cpu
|
||||
regionSize += PageSize;
|
||||
}
|
||||
|
||||
regions.Add(new HostMemoryRange(regionStart, regionSize));
|
||||
regions.Add(new MemoryRange(regionStart, regionSize));
|
||||
|
||||
return regions;
|
||||
}
|
||||
@ -386,18 +407,22 @@ namespace Ryujinx.Cpu
|
||||
|
||||
if ((va & PageMask) != 0)
|
||||
{
|
||||
ulong pa = GetPhysicalAddressInternal(va);
|
||||
|
||||
size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
|
||||
|
||||
GetHostSpanContiguous(va, size).CopyTo(data.Slice(0, size));
|
||||
_backingMemory.GetSpan(pa, size).CopyTo(data.Slice(0, size));
|
||||
|
||||
offset += size;
|
||||
}
|
||||
|
||||
for (; offset < data.Length; offset += size)
|
||||
{
|
||||
ulong pa = GetPhysicalAddressInternal(va + (ulong)offset);
|
||||
|
||||
size = Math.Min(data.Length - offset, PageSize);
|
||||
|
||||
GetHostSpanContiguous(va + (ulong)offset, size).CopyTo(data.Slice(offset, size));
|
||||
_backingMemory.GetSpan(pa, size).CopyTo(data.Slice(offset, size));
|
||||
}
|
||||
}
|
||||
catch (InvalidMemoryRegionException)
|
||||
@ -446,7 +471,7 @@ namespace Ryujinx.Cpu
|
||||
return false;
|
||||
}
|
||||
|
||||
return _pageTable.Read<nuint>((va / PageSize) * PteSize) != 0;
|
||||
return _pageTable.Read<ulong>((va / PageSize) * PteSize) != 0;
|
||||
}
|
||||
|
||||
private bool ValidateAddress(ulong va)
|
||||
@ -480,37 +505,20 @@ namespace Ryujinx.Cpu
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a span representing the given virtual address and size range in host memory.
|
||||
/// This function assumes that the requested virtual memory region is contiguous.
|
||||
/// </summary>
|
||||
/// <param name="va">Virtual address of the range</param>
|
||||
/// <param name="size">Size of the range in bytes</param>
|
||||
/// <returns>A span representing the given virtual range in host memory</returns>
|
||||
/// <exception cref="InvalidMemoryRegionException">Throw when the base virtual address is not mapped</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private unsafe Span<byte> GetHostSpanContiguous(ulong va, int size)
|
||||
private ulong GetPhysicalAddress(ulong va)
|
||||
{
|
||||
return new Span<byte>((void*)GetHostAddress(va), size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the host address for a given virtual address, using the page table.
|
||||
/// </summary>
|
||||
/// <param name="va">Virtual address</param>
|
||||
/// <returns>The corresponding host address for the given virtual address</returns>
|
||||
/// <exception cref="InvalidMemoryRegionException">Throw when the virtual address is not mapped</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private nuint GetHostAddress(ulong va)
|
||||
{
|
||||
nuint pageBase = _pageTable.Read<nuint>((va / PageSize) * PteSize) & unchecked((nuint)0xffff_ffff_ffffUL);
|
||||
|
||||
if (pageBase == 0)
|
||||
// We return -1L if the virtual address is invalid or unmapped.
|
||||
if (!ValidateAddress(va) || !IsMapped(va))
|
||||
{
|
||||
ThrowInvalidMemoryRegionException($"Not mapped: va=0x{va:X16}");
|
||||
return ulong.MaxValue;
|
||||
}
|
||||
|
||||
return pageBase + (nuint)(va & PageMask);
|
||||
return GetPhysicalAddressInternal(va);
|
||||
}
|
||||
|
||||
private ulong GetPhysicalAddressInternal(ulong va)
|
||||
{
|
||||
return PteToPa(_pageTable.Read<ulong>((va / PageSize) * PteSize) & ~(0xffffUL << 48)) + (va & PageMask);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@ -604,6 +612,16 @@ namespace Ryujinx.Cpu
|
||||
}
|
||||
}
|
||||
|
||||
private ulong PaToPte(ulong pa)
|
||||
{
|
||||
return (ulong)_backingMemory.GetPointer(pa, PageSize);
|
||||
}
|
||||
|
||||
private ulong PteToPa(ulong pte)
|
||||
{
|
||||
return (ulong)((long)pte - _backingMemory.Pointer.ToInt64());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes of resources used by the memory manager.
|
||||
/// </summary>
|
||||
|
@ -5,7 +5,6 @@ using Ryujinx.Memory.Range;
|
||||
using Ryujinx.Memory.Tracking;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
|
||||
@ -14,7 +13,7 @@ namespace Ryujinx.Cpu
|
||||
/// <summary>
|
||||
/// Represents a CPU memory manager which maps guest virtual memory directly onto a host virtual region.
|
||||
/// </summary>
|
||||
public class MemoryManagerHostMapped : MemoryManagerBase, IMemoryManager, IVirtualMemoryManagerTracked
|
||||
public class MemoryManagerHostMapped : MemoryManagerBase, IMemoryManager, IVirtualMemoryManagerTracked, IWritableBlock
|
||||
{
|
||||
public const int PageBits = 12;
|
||||
public const int PageSize = 1 << PageBits;
|
||||
@ -42,9 +41,12 @@ namespace Ryujinx.Cpu
|
||||
private readonly MemoryBlock _addressSpaceMirror;
|
||||
private readonly ulong _addressSpaceSize;
|
||||
|
||||
private readonly MemoryBlock _backingMemory;
|
||||
private readonly PageTable<ulong> _pageTable;
|
||||
|
||||
private readonly MemoryEhMeilleure _memoryEh;
|
||||
|
||||
private ulong[] _pageTable;
|
||||
private readonly ulong[] _pageBitmap;
|
||||
|
||||
public int AddressSpaceBits { get; }
|
||||
|
||||
@ -59,11 +61,14 @@ namespace Ryujinx.Cpu
|
||||
/// <summary>
|
||||
/// Creates a new instance of the host mapped memory manager.
|
||||
/// </summary>
|
||||
/// <param name="backingMemory">Physical backing memory where virtual memory will be mapped to</param>
|
||||
/// <param name="addressSpaceSize">Size of the address space</param>
|
||||
/// <param name="unsafeMode">True if unmanaged access should not be masked (unsafe), false otherwise.</param>
|
||||
/// <param name="invalidAccessHandler">Optional function to handle invalid memory accesses</param>
|
||||
public MemoryManagerHostMapped(ulong addressSpaceSize, bool unsafeMode, InvalidAccessHandler invalidAccessHandler = null)
|
||||
public MemoryManagerHostMapped(MemoryBlock backingMemory, ulong addressSpaceSize, bool unsafeMode, InvalidAccessHandler invalidAccessHandler = null)
|
||||
{
|
||||
_backingMemory = backingMemory;
|
||||
_pageTable = new PageTable<ulong>();
|
||||
_invalidAccessHandler = invalidAccessHandler;
|
||||
_unsafeMode = unsafeMode;
|
||||
_addressSpaceSize = addressSpaceSize;
|
||||
@ -79,9 +84,13 @@ namespace Ryujinx.Cpu
|
||||
|
||||
AddressSpaceBits = asBits;
|
||||
|
||||
_pageTable = new ulong[1 << (AddressSpaceBits - (PageBits + PageToPteShift))];
|
||||
_addressSpace = new MemoryBlock(asSize, MemoryAllocationFlags.Reserve | MemoryAllocationFlags.Mirrorable);
|
||||
_addressSpaceMirror = _addressSpace.CreateMirror();
|
||||
_pageBitmap = new ulong[1 << (AddressSpaceBits - (PageBits + PageToPteShift))];
|
||||
|
||||
MemoryAllocationFlags asFlags = MemoryAllocationFlags.Reserve | MemoryAllocationFlags.ViewCompatible;
|
||||
|
||||
_addressSpace = new MemoryBlock(asSize, asFlags);
|
||||
_addressSpaceMirror = new MemoryBlock(asSize, asFlags | MemoryAllocationFlags.ForceWindows4KBViewMapping);
|
||||
|
||||
Tracking = new MemoryTracking(this, PageSize, invalidAccessHandler);
|
||||
_memoryEh = new MemoryEhMeilleure(_addressSpace, Tracking);
|
||||
}
|
||||
@ -136,12 +145,14 @@ namespace Ryujinx.Cpu
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Map(ulong va, nuint hostAddress, ulong size)
|
||||
public void Map(ulong va, ulong pa, ulong size)
|
||||
{
|
||||
AssertValidAddressAndSize(va, size);
|
||||
|
||||
_addressSpace.Commit(va, size);
|
||||
_addressSpace.MapView(_backingMemory, pa, va, size);
|
||||
_addressSpaceMirror.MapView(_backingMemory, pa, va, size);
|
||||
AddMapping(va, size);
|
||||
PtMap(va, pa, size);
|
||||
|
||||
Tracking.Map(va, size);
|
||||
}
|
||||
@ -155,7 +166,32 @@ namespace Ryujinx.Cpu
|
||||
Tracking.Unmap(va, size);
|
||||
|
||||
RemoveMapping(va, size);
|
||||
_addressSpace.Decommit(va, size);
|
||||
PtUnmap(va, size);
|
||||
_addressSpace.UnmapView(_backingMemory, va, size);
|
||||
_addressSpaceMirror.UnmapView(_backingMemory, va, size);
|
||||
}
|
||||
|
||||
private void PtMap(ulong va, ulong pa, ulong size)
|
||||
{
|
||||
while (size != 0)
|
||||
{
|
||||
_pageTable.Map(va, pa);
|
||||
|
||||
va += PageSize;
|
||||
pa += PageSize;
|
||||
size -= PageSize;
|
||||
}
|
||||
}
|
||||
|
||||
private void PtUnmap(ulong va, ulong size)
|
||||
{
|
||||
while (size != 0)
|
||||
{
|
||||
_pageTable.Unmap(va);
|
||||
|
||||
va += PageSize;
|
||||
size -= PageSize;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@ -216,6 +252,7 @@ namespace Ryujinx.Cpu
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Write<T>(ulong va, T value) where T : unmanaged
|
||||
{
|
||||
@ -267,7 +304,7 @@ namespace Ryujinx.Cpu
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false)
|
||||
@ -322,7 +359,7 @@ namespace Ryujinx.Cpu
|
||||
int bit = (int)((page & 31) << 1);
|
||||
|
||||
int pageIndex = (int)(page >> PageToPteShift);
|
||||
ref ulong pageRef = ref _pageTable[pageIndex];
|
||||
ref ulong pageRef = ref _pageBitmap[pageIndex];
|
||||
|
||||
ulong pte = Volatile.Read(ref pageRef);
|
||||
|
||||
@ -373,7 +410,7 @@ namespace Ryujinx.Cpu
|
||||
mask &= endMask;
|
||||
}
|
||||
|
||||
ref ulong pageRef = ref _pageTable[pageIndex++];
|
||||
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
||||
ulong pte = Volatile.Read(ref pageRef);
|
||||
|
||||
pte |= pte >> 1;
|
||||
@ -389,16 +426,53 @@ namespace Ryujinx.Cpu
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<HostMemoryRange> GetPhysicalRegions(ulong va, ulong size)
|
||||
public IEnumerable<MemoryRange> GetPhysicalRegions(ulong va, ulong size)
|
||||
{
|
||||
if (size == 0)
|
||||
int pages = GetPagesCount(va, (uint)size, out va);
|
||||
|
||||
var regions = new List<MemoryRange>();
|
||||
|
||||
ulong regionStart = GetPhysicalAddressChecked(va);
|
||||
ulong regionSize = PageSize;
|
||||
|
||||
for (int page = 0; page < pages - 1; page++)
|
||||
{
|
||||
return Enumerable.Empty<HostMemoryRange>();
|
||||
if (!ValidateAddress(va + PageSize))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
ulong newPa = GetPhysicalAddressChecked(va + PageSize);
|
||||
|
||||
if (GetPhysicalAddressChecked(va) + PageSize != newPa)
|
||||
{
|
||||
regions.Add(new MemoryRange(regionStart, regionSize));
|
||||
regionStart = newPa;
|
||||
regionSize = 0;
|
||||
}
|
||||
|
||||
va += PageSize;
|
||||
regionSize += PageSize;
|
||||
}
|
||||
|
||||
AssertMapped(va, size);
|
||||
regions.Add(new MemoryRange(regionStart, regionSize));
|
||||
|
||||
return new HostMemoryRange[] { new HostMemoryRange(_addressSpaceMirror.GetPointer(va, size), size) };
|
||||
return regions;
|
||||
}
|
||||
|
||||
private ulong GetPhysicalAddressChecked(ulong va)
|
||||
{
|
||||
if (!IsMapped(va))
|
||||
{
|
||||
ThrowInvalidMemoryRegionException($"Not mapped: va=0x{va:X16}");
|
||||
}
|
||||
|
||||
return GetPhysicalAddressInternal(va);
|
||||
}
|
||||
|
||||
private ulong GetPhysicalAddressInternal(ulong va)
|
||||
{
|
||||
return _pageTable.Read(va) + (va & PageMask);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@ -427,7 +501,7 @@ namespace Ryujinx.Cpu
|
||||
int bit = (int)((pageStart & 31) << 1);
|
||||
|
||||
int pageIndex = (int)(pageStart >> PageToPteShift);
|
||||
ref ulong pageRef = ref _pageTable[pageIndex];
|
||||
ref ulong pageRef = ref _pageBitmap[pageIndex];
|
||||
|
||||
ulong pte = Volatile.Read(ref pageRef);
|
||||
ulong state = ((pte >> bit) & 3);
|
||||
@ -459,7 +533,7 @@ namespace Ryujinx.Cpu
|
||||
mask &= endMask;
|
||||
}
|
||||
|
||||
ref ulong pageRef = ref _pageTable[pageIndex++];
|
||||
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
||||
|
||||
ulong pte = Volatile.Read(ref pageRef);
|
||||
ulong mappedMask = mask & BlockMappedMask;
|
||||
@ -530,7 +604,7 @@ namespace Ryujinx.Cpu
|
||||
ulong tag = protTag << bit;
|
||||
|
||||
int pageIndex = (int)(pageStart >> PageToPteShift);
|
||||
ref ulong pageRef = ref _pageTable[pageIndex];
|
||||
ref ulong pageRef = ref _pageBitmap[pageIndex];
|
||||
|
||||
ulong pte;
|
||||
|
||||
@ -562,7 +636,7 @@ namespace Ryujinx.Cpu
|
||||
mask &= endMask;
|
||||
}
|
||||
|
||||
ref ulong pageRef = ref _pageTable[pageIndex++];
|
||||
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
||||
|
||||
ulong pte;
|
||||
ulong mappedMask;
|
||||
@ -632,7 +706,7 @@ namespace Ryujinx.Cpu
|
||||
mask &= endMask;
|
||||
}
|
||||
|
||||
ref ulong pageRef = ref _pageTable[pageIndex++];
|
||||
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
||||
|
||||
ulong pte;
|
||||
ulong mappedMask;
|
||||
@ -677,7 +751,7 @@ namespace Ryujinx.Cpu
|
||||
mask |= endMask;
|
||||
}
|
||||
|
||||
ref ulong pageRef = ref _pageTable[pageIndex++];
|
||||
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
||||
ulong pte;
|
||||
|
||||
do
|
||||
@ -695,11 +769,11 @@ namespace Ryujinx.Cpu
|
||||
/// </summary>
|
||||
protected override void Destroy()
|
||||
{
|
||||
_addressSpaceMirror.Dispose();
|
||||
_addressSpace.Dispose();
|
||||
_addressSpaceMirror.Dispose();
|
||||
_memoryEh.Dispose();
|
||||
}
|
||||
|
||||
private void ThrowInvalidMemoryRegionException(string message) => throw new InvalidMemoryRegionException(message);
|
||||
private static void ThrowInvalidMemoryRegionException(string message) => throw new InvalidMemoryRegionException(message);
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,15 @@ namespace Ryujinx.Graphics.GAL
|
||||
DecrementAndClamp,
|
||||
Invert,
|
||||
IncrementAndWrap,
|
||||
DecrementAndWrap
|
||||
DecrementAndWrap,
|
||||
|
||||
ZeroGl = 0x0,
|
||||
InvertGl = 0x150a,
|
||||
KeepGl = 0x1e00,
|
||||
ReplaceGl = 0x1e01,
|
||||
IncrementAndClampGl = 0x1e02,
|
||||
DecrementAndClampGl = 0x1e03,
|
||||
IncrementAndWrapGl = 0x8507,
|
||||
DecrementAndWrapGl = 0x8508
|
||||
}
|
||||
}
|
@ -928,7 +928,7 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
|
||||
|
||||
size = endAddress.Pack() - address + 1;
|
||||
|
||||
if (stride > 0 && indexTypeSmall)
|
||||
if (stride > 0 && indexTypeSmall && _drawState.DrawIndexed && !instanced)
|
||||
{
|
||||
// If the index type is a small integer type, then we might be still able
|
||||
// to reduce the vertex buffer size based on the maximum possible index value.
|
||||
|
@ -1393,6 +1393,12 @@ namespace Ryujinx.Graphics.Gpu.Image
|
||||
/// <param name="size">The size of the flushing memory access</param>
|
||||
public void FlushAction(TextureGroupHandle handle, ulong address, ulong size)
|
||||
{
|
||||
// There is a small gap here where the action is removed but _actionRegistered is still 1.
|
||||
// In this case it will skip registering the action, but here we are already handling it,
|
||||
// so there shouldn't be any issue as it's the same handler for all actions.
|
||||
|
||||
handle.ClearActionRegistered();
|
||||
|
||||
if (!handle.Modified)
|
||||
{
|
||||
return;
|
||||
|
@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
namespace Ryujinx.Graphics.Gpu.Image
|
||||
{
|
||||
@ -32,9 +33,9 @@ namespace Ryujinx.Graphics.Gpu.Image
|
||||
private ulong _modifiedSync;
|
||||
|
||||
/// <summary>
|
||||
/// Whether a tracking action is currently registered or not.
|
||||
/// Whether a tracking action is currently registered or not. (0/1)
|
||||
/// </summary>
|
||||
private bool _actionRegistered;
|
||||
private int _actionRegistered;
|
||||
|
||||
/// <summary>
|
||||
/// Whether a sync action is currently registered or not.
|
||||
@ -171,11 +172,9 @@ namespace Ryujinx.Graphics.Gpu.Image
|
||||
_syncActionRegistered = true;
|
||||
}
|
||||
|
||||
if (!_actionRegistered)
|
||||
if (Interlocked.Exchange(ref _actionRegistered, 1) == 0)
|
||||
{
|
||||
_group.RegisterAction(this);
|
||||
|
||||
_actionRegistered = true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -233,8 +232,6 @@ namespace Ryujinx.Graphics.Gpu.Image
|
||||
/// <param name="context">The GPU context used to wait for sync</param>
|
||||
public void Sync(GpuContext context)
|
||||
{
|
||||
_actionRegistered = false;
|
||||
|
||||
bool needsSync = !context.IsGpuThread();
|
||||
|
||||
if (needsSync)
|
||||
@ -263,21 +260,39 @@ namespace Ryujinx.Graphics.Gpu.Image
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the action registered variable, indicating that the tracking action should be
|
||||
/// re-registered on the next modification.
|
||||
/// </summary>
|
||||
public void ClearActionRegistered()
|
||||
{
|
||||
Interlocked.Exchange(ref _actionRegistered, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Action to perform when a sync number is registered after modification.
|
||||
/// This action will register a read tracking action on the memory tracking handle so that a flush from CPU can happen.
|
||||
/// </summary>
|
||||
private void SyncAction()
|
||||
{
|
||||
// The storage will need to signal modified again to update the sync number in future.
|
||||
_group.Storage.SignalModifiedDirty();
|
||||
|
||||
lock (Overlaps)
|
||||
{
|
||||
foreach (Texture texture in Overlaps)
|
||||
{
|
||||
texture.SignalModifiedDirty();
|
||||
}
|
||||
}
|
||||
|
||||
// Register region tracking for CPU? (again)
|
||||
_registeredSync = _modifiedSync;
|
||||
_syncActionRegistered = false;
|
||||
|
||||
if (!_actionRegistered)
|
||||
if (Interlocked.Exchange(ref _actionRegistered, 1) == 0)
|
||||
{
|
||||
_group.RegisterAction(this);
|
||||
|
||||
_actionRegistered = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -379,20 +379,28 @@ namespace Ryujinx.Graphics.OpenGL
|
||||
switch (op)
|
||||
{
|
||||
case GAL.StencilOp.Keep:
|
||||
case GAL.StencilOp.KeepGl:
|
||||
return OpenTK.Graphics.OpenGL.StencilOp.Keep;
|
||||
case GAL.StencilOp.Zero:
|
||||
case GAL.StencilOp.ZeroGl:
|
||||
return OpenTK.Graphics.OpenGL.StencilOp.Zero;
|
||||
case GAL.StencilOp.Replace:
|
||||
case GAL.StencilOp.ReplaceGl:
|
||||
return OpenTK.Graphics.OpenGL.StencilOp.Replace;
|
||||
case GAL.StencilOp.IncrementAndClamp:
|
||||
case GAL.StencilOp.IncrementAndClampGl:
|
||||
return OpenTK.Graphics.OpenGL.StencilOp.Incr;
|
||||
case GAL.StencilOp.DecrementAndClamp:
|
||||
case GAL.StencilOp.DecrementAndClampGl:
|
||||
return OpenTK.Graphics.OpenGL.StencilOp.Decr;
|
||||
case GAL.StencilOp.Invert:
|
||||
case GAL.StencilOp.InvertGl:
|
||||
return OpenTK.Graphics.OpenGL.StencilOp.Invert;
|
||||
case GAL.StencilOp.IncrementAndWrap:
|
||||
case GAL.StencilOp.IncrementAndWrapGl:
|
||||
return OpenTK.Graphics.OpenGL.StencilOp.IncrWrap;
|
||||
case GAL.StencilOp.DecrementAndWrap:
|
||||
case GAL.StencilOp.DecrementAndWrapGl:
|
||||
return OpenTK.Graphics.OpenGL.StencilOp.DecrWrap;
|
||||
}
|
||||
|
||||
|
@ -17,12 +17,14 @@ using Ryujinx.Common.Logging;
|
||||
using Ryujinx.HLE.FileSystem;
|
||||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
using Ryujinx.HLE.Loaders.Executables;
|
||||
using Ryujinx.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
using static LibHac.Fs.ApplicationSaveDataManagement;
|
||||
using static Ryujinx.HLE.HOS.ModLoader;
|
||||
@ -85,8 +87,8 @@ namespace Ryujinx.HLE.HOS
|
||||
MetaLoader metaData = ReadNpdm(codeFs);
|
||||
|
||||
_device.Configuration.VirtualFileSystem.ModLoader.CollectMods(
|
||||
new[] { TitleId },
|
||||
_device.Configuration.VirtualFileSystem.ModLoader.GetModsBasePath(),
|
||||
new[] { TitleId },
|
||||
_device.Configuration.VirtualFileSystem.ModLoader.GetModsBasePath(),
|
||||
_device.Configuration.VirtualFileSystem.ModLoader.GetSdModsBasePath());
|
||||
|
||||
if (TitleId != 0)
|
||||
@ -307,6 +309,94 @@ namespace Ryujinx.HLE.HOS
|
||||
LoadNca(nca, null, null);
|
||||
}
|
||||
|
||||
public void LoadServiceNca(string ncaFile)
|
||||
{
|
||||
// Disable PPTC here as it does not support multiple processes running.
|
||||
// TODO: This should be eventually removed and it should stop using global state and
|
||||
// instead manage the cache per process.
|
||||
Ptc.Close();
|
||||
PtcProfiler.Stop();
|
||||
|
||||
FileStream file = new FileStream(ncaFile, FileMode.Open, FileAccess.Read);
|
||||
Nca mainNca = new Nca(_device.Configuration.VirtualFileSystem.KeySet, file.AsStorage(false));
|
||||
|
||||
if (mainNca.Header.ContentType != NcaContentType.Program)
|
||||
{
|
||||
Logger.Error?.Print(LogClass.Loader, "Selected NCA is not a \"Program\" NCA");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
IFileSystem codeFs = null;
|
||||
|
||||
if (mainNca.CanOpenSection(NcaSectionType.Code))
|
||||
{
|
||||
codeFs = mainNca.OpenFileSystem(NcaSectionType.Code, _device.System.FsIntegrityCheckLevel);
|
||||
}
|
||||
|
||||
if (codeFs == null)
|
||||
{
|
||||
Logger.Error?.Print(LogClass.Loader, "No ExeFS found in NCA");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
using var npdmFile = new UniqueRef<IFile>();
|
||||
|
||||
Result result = codeFs.OpenFile(ref npdmFile.Ref(), "/main.npdm".ToU8Span(), OpenMode.Read);
|
||||
|
||||
MetaLoader metaData;
|
||||
|
||||
npdmFile.Get.GetSize(out long fileSize).ThrowIfFailure();
|
||||
|
||||
var npdmBuffer = new byte[fileSize];
|
||||
npdmFile.Get.Read(out _, 0, npdmBuffer).ThrowIfFailure();
|
||||
|
||||
metaData = new MetaLoader();
|
||||
metaData.Load(npdmBuffer).ThrowIfFailure();
|
||||
|
||||
NsoExecutable[] nsos = new NsoExecutable[ExeFsPrefixes.Length];
|
||||
|
||||
for (int i = 0; i < nsos.Length; i++)
|
||||
{
|
||||
string name = ExeFsPrefixes[i];
|
||||
|
||||
if (!codeFs.FileExists($"/{name}"))
|
||||
{
|
||||
continue; // File doesn't exist, skip.
|
||||
}
|
||||
|
||||
Logger.Info?.Print(LogClass.Loader, $"Loading {name}...");
|
||||
|
||||
using var nsoFile = new UniqueRef<IFile>();
|
||||
|
||||
codeFs.OpenFile(ref nsoFile.Ref(), $"/{name}".ToU8Span(), OpenMode.Read).ThrowIfFailure();
|
||||
|
||||
nsos[i] = new NsoExecutable(nsoFile.Release().AsStorage(), name);
|
||||
}
|
||||
|
||||
// Collect the nsos, ignoring ones that aren't used.
|
||||
NsoExecutable[] programs = nsos.Where(x => x != null).ToArray();
|
||||
|
||||
MemoryManagerMode memoryManagerMode = _device.Configuration.MemoryManagerMode;
|
||||
|
||||
if (!MemoryBlock.SupportsFlags(MemoryAllocationFlags.ViewCompatible))
|
||||
{
|
||||
memoryManagerMode = MemoryManagerMode.SoftwarePageTable;
|
||||
}
|
||||
|
||||
metaData.GetNpdm(out Npdm npdm).ThrowIfFailure();
|
||||
ProgramInfo programInfo = new ProgramInfo(in npdm, allowCodeMemoryForJit: false);
|
||||
ProgramLoader.LoadNsos(_device.System.KernelContext, out _, metaData, programInfo, executables: programs);
|
||||
|
||||
string titleIdText = npdm.Aci.Value.ProgramId.Value.ToString("x16");
|
||||
bool titleIs64Bit = (npdm.Meta.Value.Flags & 1) != 0;
|
||||
|
||||
string programName = Encoding.ASCII.GetString(npdm.Meta.Value.ProgramName).TrimEnd('\0');
|
||||
|
||||
Logger.Info?.Print(LogClass.Loader, $"Service Loaded: {programName} [{titleIdText}] [{(titleIs64Bit ? "64-bit" : "32-bit")}]");
|
||||
}
|
||||
|
||||
private void LoadNca(Nca mainNca, Nca patchNca, Nca controlNca)
|
||||
{
|
||||
if (mainNca.Header.ContentType != NcaContentType.Program)
|
||||
@ -392,8 +482,8 @@ namespace Ryujinx.HLE.HOS
|
||||
MetaLoader metaData = ReadNpdm(codeFs);
|
||||
|
||||
_device.Configuration.VirtualFileSystem.ModLoader.CollectMods(
|
||||
_device.Configuration.ContentManager.GetAocTitleIds().Prepend(TitleId),
|
||||
_device.Configuration.VirtualFileSystem.ModLoader.GetModsBasePath(),
|
||||
_device.Configuration.ContentManager.GetAocTitleIds().Prepend(TitleId),
|
||||
_device.Configuration.VirtualFileSystem.ModLoader.GetModsBasePath(),
|
||||
_device.Configuration.VirtualFileSystem.ModLoader.GetSdModsBasePath());
|
||||
|
||||
if (controlNca != null)
|
||||
@ -507,7 +597,7 @@ namespace Ryujinx.HLE.HOS
|
||||
{
|
||||
if (_device.Configuration.VirtualFileSystem.ModLoader.ReplaceExefsPartition(TitleId, ref codeFs))
|
||||
{
|
||||
metaData = null; //TODO: Check if we should retain old npdm
|
||||
metaData = null; // TODO: Check if we should retain old npdm.
|
||||
}
|
||||
|
||||
metaData ??= ReadNpdm(codeFs);
|
||||
@ -520,7 +610,7 @@ namespace Ryujinx.HLE.HOS
|
||||
|
||||
if (!codeFs.FileExists($"/{name}"))
|
||||
{
|
||||
continue; // file doesn't exist, skip
|
||||
continue; // File doesn't exist, skip.
|
||||
}
|
||||
|
||||
Logger.Info?.Print(LogClass.Loader, $"Loading {name}...");
|
||||
@ -532,13 +622,13 @@ namespace Ryujinx.HLE.HOS
|
||||
nsos[i] = new NsoExecutable(nsoFile.Release().AsStorage(), name);
|
||||
}
|
||||
|
||||
// ExeFs file replacements
|
||||
// ExeFs file replacements.
|
||||
ModLoadResult modLoadResult = _device.Configuration.VirtualFileSystem.ModLoader.ApplyExefsMods(TitleId, nsos);
|
||||
|
||||
// collect the nsos, ignoring ones that aren't used
|
||||
// Collect the nsos, ignoring ones that aren't used.
|
||||
NsoExecutable[] programs = nsos.Where(x => x != null).ToArray();
|
||||
|
||||
// take the npdm from mods if present
|
||||
// Take the npdm from mods if present.
|
||||
if (modLoadResult.Npdm != null)
|
||||
{
|
||||
metaData = modLoadResult.Npdm;
|
||||
@ -561,10 +651,21 @@ namespace Ryujinx.HLE.HOS
|
||||
Graphics.Gpu.GraphicsConfig.TitleId = TitleIdText;
|
||||
_device.Gpu.HostInitalized.Set();
|
||||
|
||||
Ptc.Initialize(TitleIdText, DisplayVersion, usePtc, _device.Configuration.MemoryManagerMode);
|
||||
MemoryManagerMode memoryManagerMode = _device.Configuration.MemoryManagerMode;
|
||||
|
||||
if (!MemoryBlock.SupportsFlags(MemoryAllocationFlags.ViewCompatible))
|
||||
{
|
||||
memoryManagerMode = MemoryManagerMode.SoftwarePageTable;
|
||||
}
|
||||
|
||||
Ptc.Initialize(TitleIdText, DisplayVersion, usePtc, memoryManagerMode);
|
||||
|
||||
// We allow it for nx-hbloader because it can be used to launch homebrew.
|
||||
bool allowCodeMemoryForJit = TitleId == 0x010000000000100DUL;
|
||||
|
||||
metaData.GetNpdm(out Npdm npdm).ThrowIfFailure();
|
||||
ProgramLoader.LoadNsos(_device.System.KernelContext, out ProcessTamperInfo tamperInfo, metaData, new ProgramInfo(in npdm), executables: programs);
|
||||
ProgramInfo programInfo = new ProgramInfo(in npdm, allowCodeMemoryForJit);
|
||||
ProgramLoader.LoadNsos(_device.System.KernelContext, out ProcessTamperInfo tamperInfo, metaData, programInfo, executables: programs);
|
||||
|
||||
_device.Configuration.VirtualFileSystem.ModLoader.LoadCheats(TitleId, tamperInfo, _device.TamperMachine);
|
||||
}
|
||||
@ -573,7 +674,7 @@ namespace Ryujinx.HLE.HOS
|
||||
{
|
||||
MetaLoader metaData = GetDefaultNpdm();
|
||||
metaData.GetNpdm(out Npdm npdm).ThrowIfFailure();
|
||||
ProgramInfo programInfo = new ProgramInfo(in npdm);
|
||||
ProgramInfo programInfo = new ProgramInfo(in npdm, allowCodeMemoryForJit: true);
|
||||
|
||||
bool isNro = Path.GetExtension(filePath).ToLower() == ".nro";
|
||||
|
||||
@ -586,7 +687,7 @@ namespace Ryujinx.HLE.HOS
|
||||
|
||||
executable = obj;
|
||||
|
||||
// homebrew NRO can actually have some data after the actual NRO
|
||||
// Homebrew NRO can actually have some data after the actual NRO.
|
||||
if (input.Length > obj.FileSize)
|
||||
{
|
||||
input.Position = obj.FileSize;
|
||||
@ -665,7 +766,7 @@ namespace Ryujinx.HLE.HOS
|
||||
TitleIs64Bit = (npdm.Meta.Value.Flags & 1) != 0;
|
||||
_device.System.LibHacHorizonManager.ArpIReader.ApplicationId = new LibHac.ApplicationId(TitleId);
|
||||
|
||||
// Explicitly null titleid to disable the shader cache
|
||||
// Explicitly null titleid to disable the shader cache.
|
||||
Graphics.Gpu.GraphicsConfig.TitleId = null;
|
||||
_device.Gpu.HostInitalized.Set();
|
||||
|
||||
|
@ -21,15 +21,20 @@ namespace Ryujinx.HLE.HOS
|
||||
{
|
||||
MemoryManagerMode mode = context.Device.Configuration.MemoryManagerMode;
|
||||
|
||||
if (!MemoryBlock.SupportsFlags(MemoryAllocationFlags.ViewCompatible))
|
||||
{
|
||||
mode = MemoryManagerMode.SoftwarePageTable;
|
||||
}
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case MemoryManagerMode.SoftwarePageTable:
|
||||
return new ArmProcessContext<MemoryManager>(pid, _gpu, new MemoryManager(addressSpaceSize, invalidAccessHandler), for64Bit);
|
||||
return new ArmProcessContext<MemoryManager>(pid, _gpu, new MemoryManager(context.Memory, addressSpaceSize, invalidAccessHandler), for64Bit);
|
||||
|
||||
case MemoryManagerMode.HostMapped:
|
||||
case MemoryManagerMode.HostMappedUnsafe:
|
||||
bool unsafeMode = mode == MemoryManagerMode.HostMappedUnsafe;
|
||||
return new ArmProcessContext<MemoryManagerHostMapped>(pid, _gpu, new MemoryManagerHostMapped(addressSpaceSize, unsafeMode, invalidAccessHandler), for64Bit);
|
||||
return new ArmProcessContext<MemoryManagerHostMapped>(pid, _gpu, new MemoryManagerHostMapped(context.Memory, addressSpaceSize, unsafeMode, invalidAccessHandler), for64Bit);
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
|
@ -72,6 +72,8 @@ namespace Ryujinx.HLE.HOS
|
||||
|
||||
internal List<NfpDevice> NfpDevices { get; private set; }
|
||||
|
||||
internal SmRegistry SmRegistry { get; private set; }
|
||||
|
||||
internal ServerBase SmServer { get; private set; }
|
||||
internal ServerBase BsdServer { get; private set; }
|
||||
internal ServerBase AudRenServer { get; private set; }
|
||||
@ -291,7 +293,8 @@ namespace Ryujinx.HLE.HOS
|
||||
|
||||
private void InitializeServices()
|
||||
{
|
||||
SmServer = new ServerBase(KernelContext, "SmServer", () => new IUserInterface(KernelContext));
|
||||
SmRegistry = new SmRegistry();
|
||||
SmServer = new ServerBase(KernelContext, "SmServer", () => new IUserInterface(KernelContext, SmRegistry));
|
||||
|
||||
// Wait until SM server thread is done with initialization,
|
||||
// only then doing connections to SM is safe.
|
||||
@ -468,7 +471,7 @@ namespace Ryujinx.HLE.HOS
|
||||
AudioRendererManager.Dispose();
|
||||
|
||||
LibHacHorizonManager.PmClient.Fs.UnregisterProgram(LibHacHorizonManager.ApplicationClient.Os.GetCurrentProcessId().Value).ThrowIfFailure();
|
||||
|
||||
|
||||
KernelContext.Dispose();
|
||||
}
|
||||
}
|
||||
|
@ -59,5 +59,15 @@ namespace Ryujinx.HLE.HOS.Kernel
|
||||
{
|
||||
return GetCurrentThread().Owner;
|
||||
}
|
||||
|
||||
internal static KProcess GetProcessByPid(ulong pid)
|
||||
{
|
||||
if (Context.Processes.TryGetValue(pid, out KProcess process))
|
||||
{
|
||||
return process;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
168
Ryujinx.HLE/HOS/Kernel/Memory/KCodeMemory.cs
Normal file
168
Ryujinx.HLE/HOS/Kernel/Memory/KCodeMemory.cs
Normal file
@ -0,0 +1,168 @@
|
||||
using Ryujinx.Common;
|
||||
using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class KCodeMemory : KAutoObject
|
||||
{
|
||||
public KProcess Owner { get; private set; }
|
||||
private readonly KPageList _pageList;
|
||||
private readonly object _lock;
|
||||
private ulong _address;
|
||||
private bool _isOwnerMapped;
|
||||
private bool _isMapped;
|
||||
|
||||
public KCodeMemory(KernelContext context) : base(context)
|
||||
{
|
||||
_pageList = new KPageList();
|
||||
_lock = new object();
|
||||
}
|
||||
|
||||
public KernelResult Initialize(ulong address, ulong size)
|
||||
{
|
||||
Owner = KernelStatic.GetCurrentProcess();
|
||||
|
||||
KernelResult result = Owner.MemoryManager.BorrowCodeMemory(_pageList, address, size);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
Owner.CpuMemory.Fill(address, size, 0xff);
|
||||
Owner.IncrementReferenceCount();
|
||||
|
||||
_address = address;
|
||||
_isMapped = false;
|
||||
_isOwnerMapped = false;
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
public KernelResult Map(ulong address, ulong size, KMemoryPermission perm)
|
||||
{
|
||||
if (_pageList.GetPagesCount() != BitUtils.DivRoundUp(size, KPageTableBase.PageSize))
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
if (_isMapped)
|
||||
{
|
||||
return KernelResult.InvalidState;
|
||||
}
|
||||
|
||||
KProcess process = KernelStatic.GetCurrentProcess();
|
||||
|
||||
KernelResult result = process.MemoryManager.MapPages(address, _pageList, MemoryState.CodeWritable, KMemoryPermission.ReadAndWrite);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
_isMapped = true;
|
||||
}
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
public KernelResult MapToOwner(ulong address, ulong size, KMemoryPermission permission)
|
||||
{
|
||||
if (_pageList.GetPagesCount() != BitUtils.DivRoundUp(size, KPageTableBase.PageSize))
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
if (_isOwnerMapped)
|
||||
{
|
||||
return KernelResult.InvalidState;
|
||||
}
|
||||
|
||||
Debug.Assert(permission == KMemoryPermission.Read || permission == KMemoryPermission.ReadAndExecute);
|
||||
|
||||
KernelResult result = Owner.MemoryManager.MapPages(address, _pageList, MemoryState.CodeReadOnly, permission);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
_isOwnerMapped = true;
|
||||
}
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
public KernelResult Unmap(ulong address, ulong size)
|
||||
{
|
||||
if (_pageList.GetPagesCount() != BitUtils.DivRoundUp(size, KPageTableBase.PageSize))
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
KProcess process = KernelStatic.GetCurrentProcess();
|
||||
|
||||
KernelResult result = process.MemoryManager.UnmapPages(address, _pageList, MemoryState.CodeWritable);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
Debug.Assert(_isMapped);
|
||||
|
||||
_isMapped = false;
|
||||
}
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
public KernelResult UnmapFromOwner(ulong address, ulong size)
|
||||
{
|
||||
if (_pageList.GetPagesCount() != BitUtils.DivRoundUp(size, KPageTableBase.PageSize))
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
KernelResult result = Owner.MemoryManager.UnmapPages(address, _pageList, MemoryState.CodeReadOnly);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
Debug.Assert(_isOwnerMapped);
|
||||
|
||||
_isOwnerMapped = false;
|
||||
}
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
protected override void Destroy()
|
||||
{
|
||||
if (!_isMapped && !_isOwnerMapped)
|
||||
{
|
||||
ulong size = _pageList.GetPagesCount() * KPageTableBase.PageSize;
|
||||
|
||||
if (Owner.MemoryManager.UnborrowCodeMemory(_address, size, _pageList) != KernelResult.Success)
|
||||
{
|
||||
throw new InvalidOperationException("Unexpected failure restoring transfer memory attributes.");
|
||||
}
|
||||
}
|
||||
|
||||
Owner.DecrementReferenceCount();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +1,7 @@
|
||||
using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
using Ryujinx.Memory;
|
||||
using Ryujinx.Memory.Range;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
@ -12,17 +9,19 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
private readonly IVirtualMemoryManager _cpuMemory;
|
||||
|
||||
public override bool SupportsMemoryAliasing => true;
|
||||
|
||||
public KPageTable(KernelContext context, IVirtualMemoryManager cpuMemory) : base(context)
|
||||
{
|
||||
_cpuMemory = cpuMemory;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override IEnumerable<HostMemoryRange> GetPhysicalRegions(ulong va, ulong size)
|
||||
protected override void GetPhysicalRegions(ulong va, ulong size, KPageList pageList)
|
||||
{
|
||||
return _cpuMemory.GetPhysicalRegions(va, size);
|
||||
var ranges = _cpuMemory.GetPhysicalRegions(va, size);
|
||||
foreach (var range in ranges)
|
||||
{
|
||||
pageList.AddRange(range.Address + DramMemoryMap.DramBase, range.Size / PageSize);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@ -34,7 +33,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult MapMemory(ulong src, ulong dst, ulong pagesCount, KMemoryPermission oldSrcPermission, KMemoryPermission newDstPermission)
|
||||
{
|
||||
var srcRanges = GetPhysicalRegions(src, pagesCount * PageSize);
|
||||
KPageList pageList = new KPageList();
|
||||
GetPhysicalRegions(src, pagesCount * PageSize, pageList);
|
||||
|
||||
KernelResult result = Reprotect(src, pagesCount, KMemoryPermission.None);
|
||||
|
||||
@ -43,7 +43,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
return result;
|
||||
}
|
||||
|
||||
result = MapPages(dst, srcRanges, newDstPermission);
|
||||
result = MapPages(dst, pageList, newDstPermission, false, 0);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
@ -59,10 +59,13 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
ulong size = pagesCount * PageSize;
|
||||
|
||||
var srcRanges = GetPhysicalRegions(src, size);
|
||||
var dstRanges = GetPhysicalRegions(dst, size);
|
||||
KPageList srcPageList = new KPageList();
|
||||
KPageList dstPageList = new KPageList();
|
||||
|
||||
if (!dstRanges.SequenceEqual(srcRanges))
|
||||
GetPhysicalRegions(src, size, srcPageList);
|
||||
GetPhysicalRegions(dst, size, dstPageList);
|
||||
|
||||
if (!dstPageList.IsEqual(srcPageList))
|
||||
{
|
||||
return KernelResult.InvalidMemRange;
|
||||
}
|
||||
@ -78,7 +81,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
KernelResult mapResult = MapPages(dst, dstRanges, oldDstPermission);
|
||||
KernelResult mapResult = MapPages(dst, dstPageList, oldDstPermission, false, 0);
|
||||
Debug.Assert(mapResult == KernelResult.Success);
|
||||
}
|
||||
|
||||
@ -92,7 +95,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
|
||||
Context.Memory.Commit(srcPa - DramMemoryMap.DramBase, size);
|
||||
|
||||
_cpuMemory.Map(dstVa, Context.Memory.GetPointer(srcPa - DramMemoryMap.DramBase, size), size);
|
||||
_cpuMemory.Map(dstVa, srcPa - DramMemoryMap.DramBase, size);
|
||||
|
||||
if (DramMemoryMap.IsHeapPhysicalAddress(srcPa))
|
||||
{
|
||||
@ -121,7 +124,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
|
||||
Context.Memory.Commit(addr, size);
|
||||
|
||||
_cpuMemory.Map(currentVa, Context.Memory.GetPointer(addr, size), size);
|
||||
_cpuMemory.Map(currentVa, addr, size);
|
||||
|
||||
if (shouldFillPages)
|
||||
{
|
||||
@ -136,33 +139,6 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult MapPages(ulong address, IEnumerable<HostMemoryRange> ranges, KMemoryPermission permission)
|
||||
{
|
||||
ulong currentVa = address;
|
||||
|
||||
foreach (var range in ranges)
|
||||
{
|
||||
ulong size = range.Size;
|
||||
|
||||
ulong pa = GetDramAddressFromHostAddress(range.Address);
|
||||
if (pa != ulong.MaxValue)
|
||||
{
|
||||
pa += DramMemoryMap.DramBase;
|
||||
if (DramMemoryMap.IsHeapPhysicalAddress(pa))
|
||||
{
|
||||
Context.MemoryManager.IncrementPagesReferenceCount(pa, size / PageSize);
|
||||
}
|
||||
}
|
||||
|
||||
_cpuMemory.Map(currentVa, range.Address, size);
|
||||
|
||||
currentVa += size;
|
||||
}
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult Unmap(ulong address, ulong pagesCount)
|
||||
{
|
||||
@ -172,13 +148,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
|
||||
foreach (var region in regions)
|
||||
{
|
||||
ulong pa = GetDramAddressFromHostAddress(region.Address);
|
||||
if (pa == ulong.MaxValue)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
pa += DramMemoryMap.DramBase;
|
||||
ulong pa = region.Address + DramMemoryMap.DramBase;
|
||||
if (DramMemoryMap.IsHeapPhysicalAddress(pa))
|
||||
{
|
||||
pagesToClose.AddRange(pa, region.Size / PageSize);
|
||||
@ -217,15 +187,5 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
_cpuMemory.Write(va, data);
|
||||
}
|
||||
|
||||
private ulong GetDramAddressFromHostAddress(nuint hostAddress)
|
||||
{
|
||||
if (hostAddress < (nuint)(ulong)Context.Memory.Pointer || hostAddress >= (nuint)((ulong)Context.Memory.Pointer + Context.Memory.Size))
|
||||
{
|
||||
return ulong.MaxValue;
|
||||
}
|
||||
|
||||
return hostAddress - (ulong)Context.Memory.Pointer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,9 @@
|
||||
using Ryujinx.Common;
|
||||
using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
using Ryujinx.Memory.Range;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
@ -73,8 +71,6 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
|
||||
private MersenneTwister _randomNumberGenerator;
|
||||
|
||||
public abstract bool SupportsMemoryAliasing { get; }
|
||||
|
||||
private MemoryFillValue _heapFillValue;
|
||||
private MemoryFillValue _ipcFillValue;
|
||||
|
||||
@ -305,7 +301,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
TlsIoRegionStart = tlsIoRegion.Start;
|
||||
TlsIoRegionEnd = tlsIoRegion.End;
|
||||
|
||||
// TODO: Check kernel configuration via secure monitor call when implemented to set memory fill values.
|
||||
// TODO: Check kernel configuration via secure monitor call when implemented to set memory fill values.
|
||||
|
||||
_currentHeapAddr = HeapRegionStart;
|
||||
_heapCapacity = 0;
|
||||
@ -380,8 +376,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
}
|
||||
}
|
||||
|
||||
public KernelResult UnmapPages(ulong address, ulong pagesCount, IEnumerable<HostMemoryRange> ranges, MemoryState stateExpected)
|
||||
public KernelResult UnmapPages(ulong address, KPageList pageList, MemoryState stateExpected)
|
||||
{
|
||||
ulong pagesCount = pageList.GetPagesCount();
|
||||
ulong size = pagesCount * PageSize;
|
||||
|
||||
ulong endAddr = address + size;
|
||||
@ -405,9 +402,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
|
||||
lock (_blockManager)
|
||||
{
|
||||
var currentRanges = GetPhysicalRegions(address, size);
|
||||
KPageList currentPageList = new KPageList();
|
||||
|
||||
if (!currentRanges.SequenceEqual(ranges))
|
||||
GetPhysicalRegions(address, size, currentPageList);
|
||||
|
||||
if (!currentPageList.IsEqual(pageList))
|
||||
{
|
||||
return KernelResult.InvalidMemRange;
|
||||
}
|
||||
@ -1096,6 +1095,77 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
}
|
||||
}
|
||||
|
||||
public KernelResult UnmapProcessMemory(ulong dst, ulong size, KPageTableBase srcPageTable, ulong src)
|
||||
{
|
||||
lock (_blockManager)
|
||||
{
|
||||
lock (srcPageTable._blockManager)
|
||||
{
|
||||
bool success = CheckRange(
|
||||
dst,
|
||||
size,
|
||||
MemoryState.Mask,
|
||||
MemoryState.ProcessMemory,
|
||||
KMemoryPermission.ReadAndWrite,
|
||||
KMemoryPermission.ReadAndWrite,
|
||||
MemoryAttribute.Mask,
|
||||
MemoryAttribute.None,
|
||||
MemoryAttribute.IpcAndDeviceMapped,
|
||||
out _,
|
||||
out _,
|
||||
out _);
|
||||
|
||||
success &= srcPageTable.CheckRange(
|
||||
src,
|
||||
size,
|
||||
MemoryState.MapProcessAllowed,
|
||||
MemoryState.MapProcessAllowed,
|
||||
KMemoryPermission.None,
|
||||
KMemoryPermission.None,
|
||||
MemoryAttribute.Mask,
|
||||
MemoryAttribute.None,
|
||||
MemoryAttribute.IpcAndDeviceMapped,
|
||||
out _,
|
||||
out _,
|
||||
out _);
|
||||
|
||||
if (!success)
|
||||
{
|
||||
return KernelResult.InvalidMemState;
|
||||
}
|
||||
|
||||
KPageList srcPageList = new KPageList();
|
||||
KPageList dstPageList = new KPageList();
|
||||
|
||||
srcPageTable.GetPhysicalRegions(src, size, srcPageList);
|
||||
GetPhysicalRegions(dst, size, dstPageList);
|
||||
|
||||
if (!dstPageList.IsEqual(srcPageList))
|
||||
{
|
||||
return KernelResult.InvalidMemRange;
|
||||
}
|
||||
}
|
||||
|
||||
if (!_slabManager.CanAllocate(MaxBlocksNeededForInsertion))
|
||||
{
|
||||
return KernelResult.OutOfResource;
|
||||
}
|
||||
|
||||
ulong pagesCount = size / PageSize;
|
||||
|
||||
KernelResult result = Unmap(dst, pagesCount);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
_blockManager.InsertBlock(dst, pagesCount, MemoryState.Unmapped);
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
}
|
||||
|
||||
public KernelResult SetProcessMemoryPermission(ulong address, ulong size, KMemoryPermission permission)
|
||||
{
|
||||
lock (_blockManager)
|
||||
@ -1690,11 +1760,6 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
bool send,
|
||||
out ulong dst)
|
||||
{
|
||||
if (!SupportsMemoryAliasing)
|
||||
{
|
||||
throw new NotSupportedException("Memory aliasing not supported, can't map IPC buffers.");
|
||||
}
|
||||
|
||||
dst = 0;
|
||||
|
||||
if (!_slabManager.CanAllocate(MaxBlocksNeededForInsertion))
|
||||
@ -1828,7 +1893,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
ulong alignedSize = endAddrTruncated - addressRounded;
|
||||
|
||||
KernelResult result = MapPages(currentVa, srcPageTable.GetPhysicalRegions(addressRounded, alignedSize), permission);
|
||||
KPageList pageList = new KPageList();
|
||||
srcPageTable.GetPhysicalRegions(addressRounded, alignedSize, pageList);
|
||||
|
||||
KernelResult result = MapPages(currentVa, pageList, permission);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
@ -2026,6 +2094,49 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
block.RestoreIpcMappingPermission();
|
||||
}
|
||||
|
||||
public KernelResult GetPagesIfStateEquals(
|
||||
ulong address,
|
||||
ulong size,
|
||||
MemoryState stateMask,
|
||||
MemoryState stateExpected,
|
||||
KMemoryPermission permissionMask,
|
||||
KMemoryPermission permissionExpected,
|
||||
MemoryAttribute attributeMask,
|
||||
MemoryAttribute attributeExpected,
|
||||
KPageList pageList)
|
||||
{
|
||||
if (!InsideAddrSpace(address, size))
|
||||
{
|
||||
return KernelResult.InvalidMemState;
|
||||
}
|
||||
|
||||
lock (_blockManager)
|
||||
{
|
||||
if (CheckRange(
|
||||
address,
|
||||
size,
|
||||
stateMask | MemoryState.IsPoolAllocated,
|
||||
stateExpected | MemoryState.IsPoolAllocated,
|
||||
permissionMask,
|
||||
permissionExpected,
|
||||
attributeMask,
|
||||
attributeExpected,
|
||||
MemoryAttribute.IpcAndDeviceMapped,
|
||||
out _,
|
||||
out _,
|
||||
out _))
|
||||
{
|
||||
GetPhysicalRegions(address, size, pageList);
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
else
|
||||
{
|
||||
return KernelResult.InvalidMemState;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public KernelResult BorrowIpcBuffer(ulong address, ulong size)
|
||||
{
|
||||
return SetAttributesAndChangePermission(
|
||||
@ -2041,7 +2152,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
MemoryAttribute.Borrowed);
|
||||
}
|
||||
|
||||
public KernelResult BorrowTransferMemory(List<HostMemoryRange> ranges, ulong address, ulong size, KMemoryPermission permission)
|
||||
public KernelResult BorrowTransferMemory(KPageList pageList, ulong address, ulong size, KMemoryPermission permission)
|
||||
{
|
||||
return SetAttributesAndChangePermission(
|
||||
address,
|
||||
@ -2054,7 +2165,23 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
MemoryAttribute.None,
|
||||
permission,
|
||||
MemoryAttribute.Borrowed,
|
||||
ranges);
|
||||
pageList);
|
||||
}
|
||||
|
||||
public KernelResult BorrowCodeMemory(KPageList pageList, ulong address, ulong size)
|
||||
{
|
||||
return SetAttributesAndChangePermission(
|
||||
address,
|
||||
size,
|
||||
MemoryState.CodeMemoryAllowed,
|
||||
MemoryState.CodeMemoryAllowed,
|
||||
KMemoryPermission.Mask,
|
||||
KMemoryPermission.ReadAndWrite,
|
||||
MemoryAttribute.Mask,
|
||||
MemoryAttribute.None,
|
||||
KMemoryPermission.None,
|
||||
MemoryAttribute.Borrowed,
|
||||
pageList);
|
||||
}
|
||||
|
||||
private KernelResult SetAttributesAndChangePermission(
|
||||
@ -2068,7 +2195,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
MemoryAttribute attributeExpected,
|
||||
KMemoryPermission newPermission,
|
||||
MemoryAttribute attributeSetMask,
|
||||
List<HostMemoryRange> ranges = null)
|
||||
KPageList pageList = null)
|
||||
{
|
||||
if (address + size <= address || !InsideAddrSpace(address, size))
|
||||
{
|
||||
@ -2093,7 +2220,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
ulong pagesCount = size / PageSize;
|
||||
|
||||
ranges?.AddRange(GetPhysicalRegions(address, size));
|
||||
if (pageList != null)
|
||||
{
|
||||
GetPhysicalRegions(address, pagesCount * PageSize, pageList);
|
||||
}
|
||||
|
||||
if (!_slabManager.CanAllocate(MaxBlocksNeededForInsertion))
|
||||
{
|
||||
@ -2143,7 +2273,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
MemoryAttribute.Borrowed);
|
||||
}
|
||||
|
||||
public KernelResult UnborrowTransferMemory(ulong address, ulong size, List<HostMemoryRange> ranges)
|
||||
public KernelResult UnborrowTransferMemory(ulong address, ulong size, KPageList pageList)
|
||||
{
|
||||
return ClearAttributesAndChangePermission(
|
||||
address,
|
||||
@ -2156,7 +2286,23 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
MemoryAttribute.Borrowed,
|
||||
KMemoryPermission.ReadAndWrite,
|
||||
MemoryAttribute.Borrowed,
|
||||
ranges);
|
||||
pageList);
|
||||
}
|
||||
|
||||
public KernelResult UnborrowCodeMemory(ulong address, ulong size, KPageList pageList)
|
||||
{
|
||||
return ClearAttributesAndChangePermission(
|
||||
address,
|
||||
size,
|
||||
MemoryState.CodeMemoryAllowed,
|
||||
MemoryState.CodeMemoryAllowed,
|
||||
KMemoryPermission.None,
|
||||
KMemoryPermission.None,
|
||||
MemoryAttribute.Mask,
|
||||
MemoryAttribute.Borrowed,
|
||||
KMemoryPermission.ReadAndWrite,
|
||||
MemoryAttribute.Borrowed,
|
||||
pageList);
|
||||
}
|
||||
|
||||
private KernelResult ClearAttributesAndChangePermission(
|
||||
@ -2170,7 +2316,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
MemoryAttribute attributeExpected,
|
||||
KMemoryPermission newPermission,
|
||||
MemoryAttribute attributeClearMask,
|
||||
List<HostMemoryRange> ranges = null)
|
||||
KPageList pageList = null)
|
||||
{
|
||||
if (address + size <= address || !InsideAddrSpace(address, size))
|
||||
{
|
||||
@ -2195,11 +2341,13 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
ulong pagesCount = size / PageSize;
|
||||
|
||||
if (ranges != null)
|
||||
if (pageList != null)
|
||||
{
|
||||
var currentRanges = GetPhysicalRegions(address, size);
|
||||
KPageList currentPageList = new KPageList();
|
||||
|
||||
if (!currentRanges.SequenceEqual(ranges))
|
||||
GetPhysicalRegions(address, pagesCount * PageSize, currentPageList);
|
||||
|
||||
if (!currentPageList.IsEqual(pageList))
|
||||
{
|
||||
return KernelResult.InvalidMemRange;
|
||||
}
|
||||
@ -2741,8 +2889,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
/// </summary>
|
||||
/// <param name="va">Virtual address of the range</param>
|
||||
/// <param name="size">Size of the range</param>
|
||||
/// <returns>Array of physical regions</returns>
|
||||
protected abstract IEnumerable<HostMemoryRange> GetPhysicalRegions(ulong va, ulong size);
|
||||
/// <param name="pageList">Page list where the ranges will be added</param>
|
||||
protected abstract void GetPhysicalRegions(ulong va, ulong size, KPageList pageList);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a read-only span of data from CPU mapped memory.
|
||||
@ -2803,16 +2951,6 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
/// <returns>Result of the mapping operation</returns>
|
||||
protected abstract KernelResult MapPages(ulong address, KPageList pageList, KMemoryPermission permission, bool shouldFillPages = false, byte fillValue = 0);
|
||||
|
||||
/// <summary>
|
||||
/// Maps a region of memory into the specified host memory ranges.
|
||||
/// </summary>
|
||||
/// <param name="address">Destination virtual address that should be mapped</param>
|
||||
/// <param name="ranges">Ranges of host memory that should be mapped</param>
|
||||
/// <param name="permission">Permission of the region to be mapped</param>
|
||||
/// <returns>Result of the mapping operation</returns>
|
||||
/// <exception cref="NotSupportedException">The implementation does not support memory aliasing</exception>
|
||||
protected abstract KernelResult MapPages(ulong address, IEnumerable<HostMemoryRange> ranges, KMemoryPermission permission);
|
||||
|
||||
/// <summary>
|
||||
/// Unmaps a region of memory that was previously mapped with one of the page mapping methods.
|
||||
/// </summary>
|
||||
|
@ -1,139 +0,0 @@
|
||||
using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
using Ryujinx.Memory;
|
||||
using Ryujinx.Memory.Range;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class KPageTableHostMapped : KPageTableBase
|
||||
{
|
||||
private const int CopyChunckSize = 0x100000;
|
||||
|
||||
private readonly IVirtualMemoryManager _cpuMemory;
|
||||
|
||||
public override bool SupportsMemoryAliasing => false;
|
||||
|
||||
public KPageTableHostMapped(KernelContext context, IVirtualMemoryManager cpuMemory) : base(context)
|
||||
{
|
||||
_cpuMemory = cpuMemory;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override IEnumerable<HostMemoryRange> GetPhysicalRegions(ulong va, ulong size)
|
||||
{
|
||||
return _cpuMemory.GetPhysicalRegions(va, size);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override ReadOnlySpan<byte> GetSpan(ulong va, int size)
|
||||
{
|
||||
return _cpuMemory.GetSpan(va, size);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult MapMemory(ulong src, ulong dst, ulong pagesCount, KMemoryPermission oldSrcPermission, KMemoryPermission newDstPermission)
|
||||
{
|
||||
ulong size = pagesCount * PageSize;
|
||||
|
||||
_cpuMemory.Map(dst, 0, size);
|
||||
|
||||
ulong currentSize = size;
|
||||
while (currentSize > 0)
|
||||
{
|
||||
ulong copySize = Math.Min(currentSize, CopyChunckSize);
|
||||
_cpuMemory.Write(dst, _cpuMemory.GetSpan(src, (int)copySize));
|
||||
currentSize -= copySize;
|
||||
}
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult UnmapMemory(ulong dst, ulong src, ulong pagesCount, KMemoryPermission oldDstPermission, KMemoryPermission newSrcPermission)
|
||||
{
|
||||
ulong size = pagesCount * PageSize;
|
||||
|
||||
// TODO: Validation.
|
||||
|
||||
ulong currentSize = size;
|
||||
while (currentSize > 0)
|
||||
{
|
||||
ulong copySize = Math.Min(currentSize, CopyChunckSize);
|
||||
_cpuMemory.Write(src, _cpuMemory.GetSpan(dst, (int)copySize));
|
||||
currentSize -= copySize;
|
||||
}
|
||||
|
||||
_cpuMemory.Unmap(dst, size);
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult MapPages(ulong dstVa, ulong pagesCount, ulong srcPa, KMemoryPermission permission, bool shouldFillPages, byte fillValue)
|
||||
{
|
||||
_cpuMemory.Map(dstVa, 0, pagesCount * PageSize);
|
||||
|
||||
if (shouldFillPages)
|
||||
{
|
||||
_cpuMemory.Fill(dstVa, pagesCount * PageSize, fillValue);
|
||||
}
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult MapPages(ulong address, KPageList pageList, KMemoryPermission permission, bool shouldFillPages, byte fillValue)
|
||||
{
|
||||
ulong pagesCount = pageList.GetPagesCount();
|
||||
|
||||
_cpuMemory.Map(address, 0, pagesCount * PageSize);
|
||||
|
||||
if (shouldFillPages)
|
||||
{
|
||||
_cpuMemory.Fill(address, pagesCount * PageSize, fillValue);
|
||||
}
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult MapPages(ulong address, IEnumerable<HostMemoryRange> ranges, KMemoryPermission permission)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult Unmap(ulong address, ulong pagesCount)
|
||||
{
|
||||
_cpuMemory.Unmap(address, pagesCount * PageSize);
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult Reprotect(ulong address, ulong pagesCount, KMemoryPermission permission)
|
||||
{
|
||||
// TODO.
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override KernelResult ReprotectWithAttributes(ulong address, ulong pagesCount, KMemoryPermission permission)
|
||||
{
|
||||
// TODO.
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void SignalMemoryTracking(ulong va, ulong size, bool write)
|
||||
{
|
||||
_cpuMemory.SignalMemoryTracking(va, size, write);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void Write(ulong va, ReadOnlySpan<byte> data)
|
||||
{
|
||||
_cpuMemory.Write(va, data);
|
||||
}
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
class KSharedMemory : KAutoObject
|
||||
{
|
||||
private readonly SharedMemoryStorage _storage;
|
||||
private readonly KPageList _pageList;
|
||||
|
||||
private readonly ulong _ownerPid;
|
||||
|
||||
@ -20,7 +20,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
KMemoryPermission ownerPermission,
|
||||
KMemoryPermission userPermission) : base(context)
|
||||
{
|
||||
_storage = storage;
|
||||
_pageList = storage.GetPageList();
|
||||
_ownerPid = ownerPid;
|
||||
_ownerPermission = ownerPermission;
|
||||
_userPermission = userPermission;
|
||||
@ -33,10 +33,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
KProcess process,
|
||||
KMemoryPermission permission)
|
||||
{
|
||||
ulong pagesCountRounded = BitUtils.DivRoundUp(size, KPageTableBase.PageSize);
|
||||
|
||||
var pageList = _storage.GetPageList();
|
||||
if (pageList.GetPagesCount() != pagesCountRounded)
|
||||
if (_pageList.GetPagesCount() != BitUtils.DivRoundUp(size, KPageTableBase.PageSize))
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
@ -50,35 +47,17 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
return KernelResult.InvalidPermission;
|
||||
}
|
||||
|
||||
KernelResult result = memoryManager.MapPages(address, pageList, MemoryState.SharedMemory, permission);
|
||||
|
||||
if (result == KernelResult.Success && !memoryManager.SupportsMemoryAliasing)
|
||||
{
|
||||
_storage.Borrow(process, address);
|
||||
}
|
||||
|
||||
return result;
|
||||
return memoryManager.MapPages(address, _pageList, MemoryState.SharedMemory, permission);
|
||||
}
|
||||
|
||||
public KernelResult UnmapFromProcess(
|
||||
KPageTableBase memoryManager,
|
||||
ulong address,
|
||||
ulong size,
|
||||
KProcess process)
|
||||
public KernelResult UnmapFromProcess(KPageTableBase memoryManager, ulong address, ulong size, KProcess process)
|
||||
{
|
||||
ulong pagesCountRounded = BitUtils.DivRoundUp(size, KPageTableBase.PageSize);
|
||||
|
||||
var pageList = _storage.GetPageList();
|
||||
ulong pagesCount = pageList.GetPagesCount();
|
||||
|
||||
if (pagesCount != pagesCountRounded)
|
||||
if (_pageList.GetPagesCount() != BitUtils.DivRoundUp(size, KPageTableBase.PageSize))
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
|
||||
var ranges = _storage.GetRanges();
|
||||
|
||||
return memoryManager.UnmapPages(address, pagesCount, ranges, MemoryState.SharedMemory);
|
||||
return memoryManager.UnmapPages(address, _pageList, MemoryState.SharedMemory);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +1,7 @@
|
||||
using Ryujinx.Common;
|
||||
using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
using Ryujinx.Memory.Range;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
@ -14,9 +12,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
// TODO: Remove when we no longer need to read it from the owner directly.
|
||||
public KProcess Creator => _creator;
|
||||
|
||||
private readonly List<HostMemoryRange> _ranges;
|
||||
|
||||
private readonly SharedMemoryStorage _storage;
|
||||
private readonly KPageList _pageList;
|
||||
|
||||
public ulong Address { get; private set; }
|
||||
public ulong Size { get; private set; }
|
||||
@ -28,12 +24,12 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
|
||||
public KTransferMemory(KernelContext context) : base(context)
|
||||
{
|
||||
_ranges = new List<HostMemoryRange>();
|
||||
_pageList = new KPageList();
|
||||
}
|
||||
|
||||
public KTransferMemory(KernelContext context, SharedMemoryStorage storage) : base(context)
|
||||
{
|
||||
_storage = storage;
|
||||
_pageList = storage.GetPageList();
|
||||
Permission = KMemoryPermission.ReadAndWrite;
|
||||
|
||||
_hasBeenInitialized = true;
|
||||
@ -46,7 +42,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
|
||||
_creator = creator;
|
||||
|
||||
KernelResult result = creator.MemoryManager.BorrowTransferMemory(_ranges, address, size, permission);
|
||||
KernelResult result = creator.MemoryManager.BorrowTransferMemory(_pageList, address, size, permission);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
@ -71,15 +67,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
KProcess process,
|
||||
KMemoryPermission permission)
|
||||
{
|
||||
if (_storage == null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
ulong pagesCountRounded = BitUtils.DivRoundUp(size, KPageTableBase.PageSize);
|
||||
|
||||
var pageList = _storage.GetPageList();
|
||||
if (pageList.GetPagesCount() != pagesCountRounded)
|
||||
if (_pageList.GetPagesCount() != BitUtils.DivRoundUp(size, KPageTableBase.PageSize))
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
@ -91,16 +79,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
|
||||
MemoryState state = Permission == KMemoryPermission.None ? MemoryState.TransferMemoryIsolated : MemoryState.TransferMemory;
|
||||
|
||||
KernelResult result = memoryManager.MapPages(address, pageList, state, KMemoryPermission.ReadAndWrite);
|
||||
KernelResult result = memoryManager.MapPages(address, _pageList, state, KMemoryPermission.ReadAndWrite);
|
||||
|
||||
if (result == KernelResult.Success)
|
||||
{
|
||||
_isMapped = true;
|
||||
|
||||
if (!memoryManager.SupportsMemoryAliasing)
|
||||
{
|
||||
_storage.Borrow(process, address);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
@ -112,26 +95,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
ulong size,
|
||||
KProcess process)
|
||||
{
|
||||
if (_storage == null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
ulong pagesCountRounded = BitUtils.DivRoundUp(size, KPageTableBase.PageSize);
|
||||
|
||||
var pageList = _storage.GetPageList();
|
||||
ulong pagesCount = pageList.GetPagesCount();
|
||||
|
||||
if (pagesCount != pagesCountRounded)
|
||||
if (_pageList.GetPagesCount() != BitUtils.DivRoundUp(size, KPageTableBase.PageSize))
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
|
||||
var ranges = _storage.GetRanges();
|
||||
|
||||
MemoryState state = Permission == KMemoryPermission.None ? MemoryState.TransferMemoryIsolated : MemoryState.TransferMemory;
|
||||
|
||||
KernelResult result = memoryManager.UnmapPages(address, pagesCount, ranges, state);
|
||||
KernelResult result = memoryManager.UnmapPages(address, _pageList, state);
|
||||
|
||||
if (result == KernelResult.Success)
|
||||
{
|
||||
@ -145,7 +116,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
if (_hasBeenInitialized)
|
||||
{
|
||||
if (!_isMapped && _creator.MemoryManager.UnborrowTransferMemory(Address, Size, _ranges) != KernelResult.Success)
|
||||
if (!_isMapped && _creator.MemoryManager.UnborrowTransferMemory(Address, Size, _pageList) != KernelResult.Success)
|
||||
{
|
||||
throw new InvalidOperationException("Unexpected failure restoring transfer memory attributes.");
|
||||
}
|
||||
|
@ -1,8 +1,4 @@
|
||||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
using Ryujinx.Memory;
|
||||
using Ryujinx.Memory.Range;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
{
|
||||
@ -12,9 +8,6 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
private readonly KPageList _pageList;
|
||||
private readonly ulong _size;
|
||||
|
||||
private IVirtualMemoryManager _borrowerMemory;
|
||||
private ulong _borrowerVa;
|
||||
|
||||
public SharedMemoryStorage(KernelContext context, KPageList pageList)
|
||||
{
|
||||
_context = context;
|
||||
@ -29,24 +22,6 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
}
|
||||
}
|
||||
|
||||
public void Borrow(KProcess dstProcess, ulong va)
|
||||
{
|
||||
ulong currentOffset = 0;
|
||||
|
||||
foreach (KPageNode pageNode in _pageList)
|
||||
{
|
||||
ulong address = pageNode.Address - DramMemoryMap.DramBase;
|
||||
ulong size = pageNode.PagesCount * KPageTableBase.PageSize;
|
||||
|
||||
dstProcess.CpuMemory.Write(va + currentOffset, _context.Memory.GetSpan(address + currentOffset, (int)size));
|
||||
|
||||
currentOffset += size;
|
||||
}
|
||||
|
||||
_borrowerMemory = dstProcess.CpuMemory;
|
||||
_borrowerVa = va;
|
||||
}
|
||||
|
||||
public void ZeroFill()
|
||||
{
|
||||
for (ulong offset = 0; offset < _size; offset += sizeof(ulong))
|
||||
@ -57,42 +32,13 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory
|
||||
|
||||
public ref T GetRef<T>(ulong offset) where T : unmanaged
|
||||
{
|
||||
if (_borrowerMemory == null)
|
||||
if (_pageList.Nodes.Count == 1)
|
||||
{
|
||||
if (_pageList.Nodes.Count == 1)
|
||||
{
|
||||
ulong address = _pageList.Nodes.First.Value.Address - DramMemoryMap.DramBase;
|
||||
return ref _context.Memory.GetRef<T>(address + offset);
|
||||
}
|
||||
|
||||
throw new NotImplementedException("Non-contiguous shared memory is not yet supported.");
|
||||
ulong address = _pageList.Nodes.First.Value.Address - DramMemoryMap.DramBase;
|
||||
return ref _context.Memory.GetRef<T>(address + offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ref _borrowerMemory.GetRef<T>(_borrowerVa + offset);
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<HostMemoryRange> GetRanges()
|
||||
{
|
||||
if (_borrowerMemory == null)
|
||||
{
|
||||
var ranges = new List<HostMemoryRange>();
|
||||
|
||||
foreach (KPageNode pageNode in _pageList)
|
||||
{
|
||||
ulong address = pageNode.Address - DramMemoryMap.DramBase;
|
||||
ulong size = pageNode.PagesCount * KPageTableBase.PageSize;
|
||||
|
||||
ranges.Add(new HostMemoryRange(_context.Memory.GetPointer(address, size), size));
|
||||
}
|
||||
|
||||
return ranges;
|
||||
}
|
||||
else
|
||||
{
|
||||
return _borrowerMemory.GetPhysicalRegions(_borrowerVa, _size);
|
||||
}
|
||||
throw new NotImplementedException("Non-contiguous shared memory is not yet supported.");
|
||||
}
|
||||
|
||||
public KPageList GetPageList()
|
||||
|
@ -60,6 +60,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
||||
|
||||
public KProcessCapabilities Capabilities { get; private set; }
|
||||
|
||||
public bool AllowCodeMemoryForJit { get; private set; }
|
||||
|
||||
public ulong TitleId { get; private set; }
|
||||
public bool IsApplication { get; private set; }
|
||||
public ulong Pid { get; private set; }
|
||||
@ -90,7 +92,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
||||
|
||||
public HleProcessDebugger Debugger { get; private set; }
|
||||
|
||||
public KProcess(KernelContext context) : base(context)
|
||||
public KProcess(KernelContext context, bool allowCodeMemoryForJit = false) : base(context)
|
||||
{
|
||||
_processLock = new object();
|
||||
_threadingLock = new object();
|
||||
@ -102,6 +104,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
||||
|
||||
Capabilities = new KProcessCapabilities();
|
||||
|
||||
AllowCodeMemoryForJit = allowCodeMemoryForJit;
|
||||
|
||||
RandomEntropy = new ulong[KScheduler.CpuCoresCount];
|
||||
PinnedThreads = new KThread[KScheduler.CpuCoresCount];
|
||||
|
||||
@ -1076,14 +1080,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
||||
|
||||
Context = _contextFactory.Create(KernelContext, Pid, 1UL << addrSpaceBits, InvalidAccessHandler, for64Bit);
|
||||
|
||||
if (Context.AddressSpace is MemoryManagerHostMapped)
|
||||
{
|
||||
MemoryManager = new KPageTableHostMapped(KernelContext, CpuMemory);
|
||||
}
|
||||
else
|
||||
{
|
||||
MemoryManager = new KPageTable(KernelContext, CpuMemory);
|
||||
}
|
||||
MemoryManager = new KPageTable(KernelContext, CpuMemory);
|
||||
}
|
||||
|
||||
private bool InvalidAccessHandler(ulong va)
|
||||
|
@ -6,7 +6,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process
|
||||
{
|
||||
public IProcessContext Create(KernelContext context, ulong pid, ulong addressSpaceSize, InvalidAccessHandler invalidAccessHandler, bool for64Bit)
|
||||
{
|
||||
return new ProcessContext(new AddressSpaceManager(addressSpaceSize));
|
||||
return new ProcessContext(new AddressSpaceManager(context.Memory, addressSpaceSize));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
10
Ryujinx.HLE/HOS/Kernel/SupervisorCall/CodeMemoryOperation.cs
Normal file
10
Ryujinx.HLE/HOS/Kernel/SupervisorCall/CodeMemoryOperation.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
||||
{
|
||||
enum CodeMemoryOperation : uint
|
||||
{
|
||||
Map,
|
||||
MapToOwner,
|
||||
Unmap,
|
||||
UnmapFromOwner
|
||||
};
|
||||
}
|
@ -7,7 +7,6 @@ using Ryujinx.HLE.HOS.Kernel.Ipc;
|
||||
using Ryujinx.HLE.HOS.Kernel.Memory;
|
||||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
using Ryujinx.HLE.HOS.Kernel.Threading;
|
||||
using Ryujinx.Memory;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
@ -1317,6 +1316,248 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
||||
return process.MemoryManager.UnmapPhysicalMemory(address, size);
|
||||
}
|
||||
|
||||
public KernelResult CreateCodeMemory(ulong address, ulong size, out int handle)
|
||||
{
|
||||
handle = 0;
|
||||
|
||||
if (!PageAligned(address))
|
||||
{
|
||||
return KernelResult.InvalidAddress;
|
||||
}
|
||||
|
||||
if (!PageAligned(size) || size == 0)
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
|
||||
if (size + address <= address)
|
||||
{
|
||||
return KernelResult.InvalidMemState;
|
||||
}
|
||||
|
||||
KCodeMemory codeMemory = new KCodeMemory(_context);
|
||||
|
||||
using var _ = new OnScopeExit(codeMemory.DecrementReferenceCount);
|
||||
|
||||
KProcess currentProcess = KernelStatic.GetCurrentProcess();
|
||||
|
||||
if (!currentProcess.MemoryManager.InsideAddrSpace(address, size))
|
||||
{
|
||||
return KernelResult.InvalidMemState;
|
||||
}
|
||||
|
||||
KernelResult result = codeMemory.Initialize(address, size);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
return currentProcess.HandleTable.GenerateHandle(codeMemory, out handle);
|
||||
}
|
||||
|
||||
public KernelResult ControlCodeMemory(int handle, CodeMemoryOperation op, ulong address, ulong size, KMemoryPermission permission)
|
||||
{
|
||||
KProcess currentProcess = KernelStatic.GetCurrentProcess();
|
||||
|
||||
KCodeMemory codeMemory = currentProcess.HandleTable.GetObject<KCodeMemory>(handle);
|
||||
|
||||
// Newer versions of the kernel also returns an error here if the owner and process
|
||||
// where the operation will happen are the same. We do not return an error here
|
||||
// for homebrew because some of them requires this to be patched out to work (for JIT).
|
||||
if (codeMemory == null || (!currentProcess.AllowCodeMemoryForJit && codeMemory.Owner == currentProcess))
|
||||
{
|
||||
return KernelResult.InvalidHandle;
|
||||
}
|
||||
|
||||
switch (op)
|
||||
{
|
||||
case CodeMemoryOperation.Map:
|
||||
if (!currentProcess.MemoryManager.CanContain(address, size, MemoryState.CodeWritable))
|
||||
{
|
||||
return KernelResult.InvalidMemRange;
|
||||
}
|
||||
|
||||
if (permission != KMemoryPermission.ReadAndWrite)
|
||||
{
|
||||
return KernelResult.InvalidPermission;
|
||||
}
|
||||
|
||||
return codeMemory.Map(address, size, permission);
|
||||
|
||||
case CodeMemoryOperation.MapToOwner:
|
||||
if (!currentProcess.MemoryManager.CanContain(address, size, MemoryState.CodeReadOnly))
|
||||
{
|
||||
return KernelResult.InvalidMemRange;
|
||||
}
|
||||
|
||||
if (permission != KMemoryPermission.Read && permission != KMemoryPermission.ReadAndExecute)
|
||||
{
|
||||
return KernelResult.InvalidPermission;
|
||||
}
|
||||
|
||||
return codeMemory.MapToOwner(address, size, permission);
|
||||
|
||||
case CodeMemoryOperation.Unmap:
|
||||
if (!currentProcess.MemoryManager.CanContain(address, size, MemoryState.CodeWritable))
|
||||
{
|
||||
return KernelResult.InvalidMemRange;
|
||||
}
|
||||
|
||||
if (permission != KMemoryPermission.None)
|
||||
{
|
||||
return KernelResult.InvalidPermission;
|
||||
}
|
||||
|
||||
return codeMemory.Unmap(address, size);
|
||||
|
||||
case CodeMemoryOperation.UnmapFromOwner:
|
||||
if (!currentProcess.MemoryManager.CanContain(address, size, MemoryState.CodeReadOnly))
|
||||
{
|
||||
return KernelResult.InvalidMemRange;
|
||||
}
|
||||
|
||||
if (permission != KMemoryPermission.None)
|
||||
{
|
||||
return KernelResult.InvalidPermission;
|
||||
}
|
||||
|
||||
return codeMemory.UnmapFromOwner(address, size);
|
||||
|
||||
default: return KernelResult.InvalidEnumValue;
|
||||
}
|
||||
}
|
||||
|
||||
public KernelResult SetProcessMemoryPermission(int handle, ulong src, ulong size, KMemoryPermission permission)
|
||||
{
|
||||
if (!PageAligned(src))
|
||||
{
|
||||
return KernelResult.InvalidAddress;
|
||||
}
|
||||
|
||||
if (!PageAligned(size) || size == 0)
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
|
||||
if (permission != KMemoryPermission.None &&
|
||||
permission != KMemoryPermission.Read &&
|
||||
permission != KMemoryPermission.ReadAndWrite &&
|
||||
permission != KMemoryPermission.ReadAndExecute)
|
||||
{
|
||||
return KernelResult.InvalidPermission;
|
||||
}
|
||||
|
||||
KProcess currentProcess = KernelStatic.GetCurrentProcess();
|
||||
|
||||
KProcess targetProcess = currentProcess.HandleTable.GetObject<KProcess>(handle);
|
||||
|
||||
if (targetProcess == null)
|
||||
{
|
||||
return KernelResult.InvalidHandle;
|
||||
}
|
||||
|
||||
if (targetProcess.MemoryManager.OutsideAddrSpace(src, size))
|
||||
{
|
||||
return KernelResult.InvalidMemState;
|
||||
}
|
||||
|
||||
return targetProcess.MemoryManager.SetProcessMemoryPermission(src, size, permission);
|
||||
}
|
||||
|
||||
public KernelResult MapProcessMemory(ulong dst, int handle, ulong src, ulong size)
|
||||
{
|
||||
if (!PageAligned(src) || !PageAligned(dst))
|
||||
{
|
||||
return KernelResult.InvalidAddress;
|
||||
}
|
||||
|
||||
if (!PageAligned(size) || size == 0)
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
|
||||
if (dst + size <= dst || src + size <= src)
|
||||
{
|
||||
return KernelResult.InvalidMemRange;
|
||||
}
|
||||
|
||||
KProcess dstProcess = KernelStatic.GetCurrentProcess();
|
||||
KProcess srcProcess = dstProcess.HandleTable.GetObject<KProcess>(handle);
|
||||
|
||||
if (srcProcess == null)
|
||||
{
|
||||
return KernelResult.InvalidHandle;
|
||||
}
|
||||
|
||||
if (!srcProcess.MemoryManager.InsideAddrSpace(src, size) ||
|
||||
!dstProcess.MemoryManager.CanContain(dst, size, MemoryState.ProcessMemory))
|
||||
{
|
||||
return KernelResult.InvalidMemRange;
|
||||
}
|
||||
|
||||
KPageList pageList = new KPageList();
|
||||
|
||||
KernelResult result = srcProcess.MemoryManager.GetPagesIfStateEquals(
|
||||
src,
|
||||
size,
|
||||
MemoryState.MapProcessAllowed,
|
||||
MemoryState.MapProcessAllowed,
|
||||
KMemoryPermission.None,
|
||||
KMemoryPermission.None,
|
||||
MemoryAttribute.Mask,
|
||||
MemoryAttribute.None,
|
||||
pageList);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
return dstProcess.MemoryManager.MapPages(dst, pageList, MemoryState.ProcessMemory, KMemoryPermission.ReadAndWrite);
|
||||
}
|
||||
|
||||
public KernelResult UnmapProcessMemory(ulong dst, int handle, ulong src, ulong size)
|
||||
{
|
||||
if (!PageAligned(src) || !PageAligned(dst))
|
||||
{
|
||||
return KernelResult.InvalidAddress;
|
||||
}
|
||||
|
||||
if (!PageAligned(size) || size == 0)
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
|
||||
if (dst + size <= dst || src + size <= src)
|
||||
{
|
||||
return KernelResult.InvalidMemRange;
|
||||
}
|
||||
|
||||
KProcess dstProcess = KernelStatic.GetCurrentProcess();
|
||||
KProcess srcProcess = dstProcess.HandleTable.GetObject<KProcess>(handle);
|
||||
|
||||
if (srcProcess == null)
|
||||
{
|
||||
return KernelResult.InvalidHandle;
|
||||
}
|
||||
|
||||
if (!srcProcess.MemoryManager.InsideAddrSpace(src, size) ||
|
||||
!dstProcess.MemoryManager.CanContain(dst, size, MemoryState.ProcessMemory))
|
||||
{
|
||||
return KernelResult.InvalidMemRange;
|
||||
}
|
||||
|
||||
KernelResult result = dstProcess.MemoryManager.UnmapProcessMemory(dst, size, srcProcess.MemoryManager, src);
|
||||
|
||||
if (result != KernelResult.Success)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
return KernelResult.Success;
|
||||
}
|
||||
|
||||
public KernelResult MapProcessCodeMemory(int handle, ulong dst, ulong src, ulong size)
|
||||
{
|
||||
if (!PageAligned(dst) || !PageAligned(src))
|
||||
@ -1391,43 +1632,6 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
||||
return targetProcess.MemoryManager.UnmapProcessCodeMemory(dst, src, size);
|
||||
}
|
||||
|
||||
public KernelResult SetProcessMemoryPermission(int handle, ulong src, ulong size, KMemoryPermission permission)
|
||||
{
|
||||
if (!PageAligned(src))
|
||||
{
|
||||
return KernelResult.InvalidAddress;
|
||||
}
|
||||
|
||||
if (!PageAligned(size) || size == 0)
|
||||
{
|
||||
return KernelResult.InvalidSize;
|
||||
}
|
||||
|
||||
if (permission != KMemoryPermission.None &&
|
||||
permission != KMemoryPermission.Read &&
|
||||
permission != KMemoryPermission.ReadAndWrite &&
|
||||
permission != KMemoryPermission.ReadAndExecute)
|
||||
{
|
||||
return KernelResult.InvalidPermission;
|
||||
}
|
||||
|
||||
KProcess currentProcess = KernelStatic.GetCurrentProcess();
|
||||
|
||||
KProcess targetProcess = currentProcess.HandleTable.GetObject<KProcess>(handle);
|
||||
|
||||
if (targetProcess == null)
|
||||
{
|
||||
return KernelResult.InvalidHandle;
|
||||
}
|
||||
|
||||
if (targetProcess.MemoryManager.OutsideAddrSpace(src, size))
|
||||
{
|
||||
return KernelResult.InvalidMemState;
|
||||
}
|
||||
|
||||
return targetProcess.MemoryManager.SetProcessMemoryPermission(src, size, permission);
|
||||
}
|
||||
|
||||
private static bool PageAligned(ulong address)
|
||||
{
|
||||
return (address & (KPageTableBase.PageSize - 1)) == 0;
|
||||
|
@ -143,6 +143,26 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
||||
return _syscall.CreateTransferMemory(out handle, address, size, permission);
|
||||
}
|
||||
|
||||
public KernelResult CreateCodeMemory32([R(1)] uint address, [R(2)] uint size, [R(1)] out int handle)
|
||||
{
|
||||
return _syscall.CreateCodeMemory(address, size, out handle);
|
||||
}
|
||||
|
||||
public KernelResult ControlCodeMemory32(
|
||||
[R(0)] int handle,
|
||||
[R(1)] CodeMemoryOperation op,
|
||||
[R(2)] uint addressLow,
|
||||
[R(3)] uint addressHigh,
|
||||
[R(4)] uint sizeLow,
|
||||
[R(5)] uint sizeHigh,
|
||||
[R(6)] KMemoryPermission permission)
|
||||
{
|
||||
ulong address = addressLow | ((ulong)addressHigh << 32);
|
||||
ulong size = sizeLow | ((ulong)sizeHigh << 32);
|
||||
|
||||
return _syscall.ControlCodeMemory(handle, op, address, size, permission);
|
||||
}
|
||||
|
||||
public KernelResult MapTransferMemory32([R(0)] int handle, [R(1)] uint address, [R(2)] uint size, [R(3)] KMemoryPermission permission)
|
||||
{
|
||||
return _syscall.MapTransferMemory(handle, address, size, permission);
|
||||
@ -163,6 +183,34 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
||||
return _syscall.UnmapPhysicalMemory(address, size);
|
||||
}
|
||||
|
||||
public KernelResult SetProcessMemoryPermission32(
|
||||
[R(0)] int handle,
|
||||
[R(1)] uint sizeLow,
|
||||
[R(2)] uint srcLow,
|
||||
[R(3)] uint srcHigh,
|
||||
[R(4)] uint sizeHigh,
|
||||
[R(5)] KMemoryPermission permission)
|
||||
{
|
||||
ulong src = srcLow | ((ulong)srcHigh << 32);
|
||||
ulong size = sizeLow | ((ulong)sizeHigh << 32);
|
||||
|
||||
return _syscall.SetProcessMemoryPermission(handle, src, size, permission);
|
||||
}
|
||||
|
||||
public KernelResult MapProcessMemory32([R(0)] uint dst, [R(1)] int handle, [R(2)] uint srcLow, [R(3)] uint srcHigh, [R(4)] uint size)
|
||||
{
|
||||
ulong src = srcLow | ((ulong)srcHigh << 32);
|
||||
|
||||
return _syscall.MapProcessMemory(dst, handle, src, size);
|
||||
}
|
||||
|
||||
public KernelResult UnmapProcessMemory32([R(0)] uint dst, [R(1)] int handle, [R(2)] uint srcLow, [R(3)] uint srcHigh, [R(4)] uint size)
|
||||
{
|
||||
ulong src = srcLow | ((ulong)srcHigh << 32);
|
||||
|
||||
return _syscall.UnmapProcessMemory(dst, handle, src, size);
|
||||
}
|
||||
|
||||
public KernelResult MapProcessCodeMemory32([R(0)] int handle, [R(1)] uint srcLow, [R(2)] uint dstLow, [R(3)] uint dstHigh, [R(4)] uint srcHigh, [R(5)] uint sizeLow, [R(6)] uint sizeHigh)
|
||||
{
|
||||
ulong src = srcLow | ((ulong)srcHigh << 32);
|
||||
@ -181,20 +229,6 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
||||
return _syscall.UnmapProcessCodeMemory(handle, dst, src, size);
|
||||
}
|
||||
|
||||
public KernelResult SetProcessMemoryPermission32(
|
||||
[R(0)] int handle,
|
||||
[R(1)] uint sizeLow,
|
||||
[R(2)] uint srcLow,
|
||||
[R(3)] uint srcHigh,
|
||||
[R(4)] uint sizeHigh,
|
||||
[R(5)] KMemoryPermission permission)
|
||||
{
|
||||
ulong src = srcLow | ((ulong)srcHigh << 32);
|
||||
ulong size = sizeLow | ((ulong)sizeHigh << 32);
|
||||
|
||||
return _syscall.SetProcessMemoryPermission(handle, src, size, permission);
|
||||
}
|
||||
|
||||
// System
|
||||
|
||||
public void ExitProcess32()
|
||||
|
@ -160,6 +160,16 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
||||
return _syscall.CreateTransferMemory(out handle, address, size, permission);
|
||||
}
|
||||
|
||||
public KernelResult CreateCodeMemory64([R(1)] ulong address, [R(2)] ulong size, [R(1)] out int handle)
|
||||
{
|
||||
return _syscall.CreateCodeMemory(address, size, out handle);
|
||||
}
|
||||
|
||||
public KernelResult ControlCodeMemory64([R(0)] int handle, [R(1)] CodeMemoryOperation op, [R(2)] ulong address, [R(3)] ulong size, [R(4)] KMemoryPermission permission)
|
||||
{
|
||||
return _syscall.ControlCodeMemory(handle, op, address, size, permission);
|
||||
}
|
||||
|
||||
public KernelResult MapTransferMemory64([R(0)] int handle, [R(1)] ulong address, [R(2)] ulong size, [R(3)] KMemoryPermission permission)
|
||||
{
|
||||
return _syscall.MapTransferMemory(handle, address, size, permission);
|
||||
@ -180,6 +190,21 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
||||
return _syscall.UnmapPhysicalMemory(address, size);
|
||||
}
|
||||
|
||||
public KernelResult SetProcessMemoryPermission64([R(0)] int handle, [R(1)] ulong src, [R(2)] ulong size, [R(3)] KMemoryPermission permission)
|
||||
{
|
||||
return _syscall.SetProcessMemoryPermission(handle, src, size, permission);
|
||||
}
|
||||
|
||||
public KernelResult MapProcessMemory64([R(0)] ulong dst, [R(1)] int handle, [R(2)] ulong src, [R(3)] ulong size)
|
||||
{
|
||||
return _syscall.MapProcessMemory(dst, handle, src, size);
|
||||
}
|
||||
|
||||
public KernelResult UnmapProcessMemory64([R(0)] ulong dst, [R(1)] int handle, [R(2)] ulong src, [R(3)] ulong size)
|
||||
{
|
||||
return _syscall.UnmapProcessMemory(dst, handle, src, size);
|
||||
}
|
||||
|
||||
public KernelResult MapProcessCodeMemory64([R(0)] int handle, [R(1)] ulong dst, [R(2)] ulong src, [R(3)] ulong size)
|
||||
{
|
||||
return _syscall.MapProcessCodeMemory(handle, dst, src, size);
|
||||
@ -190,11 +215,6 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
||||
return _syscall.UnmapProcessCodeMemory(handle, dst, src, size);
|
||||
}
|
||||
|
||||
public KernelResult SetProcessMemoryPermission64([R(0)] int handle, [R(1)] ulong src, [R(2)] ulong size, [R(3)] KMemoryPermission permission)
|
||||
{
|
||||
return _syscall.SetProcessMemoryPermission(handle, src, size, permission);
|
||||
}
|
||||
|
||||
// System
|
||||
|
||||
public void ExitProcess64()
|
||||
|
@ -78,6 +78,8 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
||||
{ 0x43, nameof(Syscall64.ReplyAndReceive64) },
|
||||
{ 0x44, nameof(Syscall64.ReplyAndReceiveWithUserBuffer64) },
|
||||
{ 0x45, nameof(Syscall64.CreateEvent64) },
|
||||
{ 0x4b, nameof(Syscall64.CreateCodeMemory64) },
|
||||
{ 0x4c, nameof(Syscall64.ControlCodeMemory64) },
|
||||
{ 0x51, nameof(Syscall64.MapTransferMemory64) },
|
||||
{ 0x52, nameof(Syscall64.UnmapTransferMemory64) },
|
||||
{ 0x65, nameof(Syscall64.GetProcessList64) },
|
||||
@ -86,6 +88,8 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
||||
{ 0x71, nameof(Syscall64.ManageNamedPort64) },
|
||||
{ 0x72, nameof(Syscall64.ConnectToPort64) },
|
||||
{ 0x73, nameof(Syscall64.SetProcessMemoryPermission64) },
|
||||
{ 0x74, nameof(Syscall64.MapProcessMemory64) },
|
||||
{ 0x75, nameof(Syscall64.UnmapProcessMemory64) },
|
||||
{ 0x77, nameof(Syscall64.MapProcessCodeMemory64) },
|
||||
{ 0x78, nameof(Syscall64.UnmapProcessCodeMemory64) },
|
||||
{ 0x7B, nameof(Syscall64.TerminateProcess64) },
|
||||
@ -152,6 +156,8 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
||||
{ 0x41, nameof(Syscall32.AcceptSession32) },
|
||||
{ 0x43, nameof(Syscall32.ReplyAndReceive32) },
|
||||
{ 0x45, nameof(Syscall32.CreateEvent32) },
|
||||
{ 0x4b, nameof(Syscall32.CreateCodeMemory32) },
|
||||
{ 0x4c, nameof(Syscall32.ControlCodeMemory32) },
|
||||
{ 0x51, nameof(Syscall32.MapTransferMemory32) },
|
||||
{ 0x52, nameof(Syscall32.UnmapTransferMemory32) },
|
||||
{ 0x5F, nameof(Syscall32.FlushProcessDataCache32) },
|
||||
@ -161,6 +167,8 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
|
||||
{ 0x71, nameof(Syscall32.ManageNamedPort32) },
|
||||
{ 0x72, nameof(Syscall32.ConnectToPort32) },
|
||||
{ 0x73, nameof(Syscall32.SetProcessMemoryPermission32) },
|
||||
{ 0x74, nameof(Syscall32.MapProcessMemory32) },
|
||||
{ 0x75, nameof(Syscall32.UnmapProcessMemory32) },
|
||||
{ 0x77, nameof(Syscall32.MapProcessCodeMemory32) },
|
||||
{ 0x78, nameof(Syscall32.UnmapProcessCodeMemory32) },
|
||||
{ 0x7B, nameof(Syscall32.TerminateProcess32) },
|
||||
|
@ -20,11 +20,13 @@ namespace Ryujinx.HLE.HOS
|
||||
{
|
||||
public string Name;
|
||||
public ulong ProgramId;
|
||||
public bool AllowCodeMemoryForJit;
|
||||
|
||||
public ProgramInfo(in Npdm npdm)
|
||||
public ProgramInfo(in Npdm npdm, bool allowCodeMemoryForJit)
|
||||
{
|
||||
Name = StringUtils.Utf8ZToString(npdm.Meta.Value.ProgramName);
|
||||
ProgramId = npdm.Aci.Value.ProgramId.Value;
|
||||
AllowCodeMemoryForJit = allowCodeMemoryForJit;
|
||||
}
|
||||
}
|
||||
|
||||
@ -141,7 +143,13 @@ namespace Ryujinx.HLE.HOS
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool LoadNsos(KernelContext context, out ProcessTamperInfo tamperInfo, MetaLoader metaData, ProgramInfo programInfo, byte[] arguments = null, params IExecutable[] executables)
|
||||
public static bool LoadNsos(
|
||||
KernelContext context,
|
||||
out ProcessTamperInfo tamperInfo,
|
||||
MetaLoader metaData,
|
||||
ProgramInfo programInfo,
|
||||
byte[] arguments = null,
|
||||
params IExecutable[] executables)
|
||||
{
|
||||
LibHac.Result rc = metaData.GetNpdm(out var npdm);
|
||||
|
||||
@ -243,7 +251,7 @@ namespace Ryujinx.HLE.HOS
|
||||
return false;
|
||||
}
|
||||
|
||||
KProcess process = new KProcess(context);
|
||||
KProcess process = new KProcess(context, programInfo.AllowCodeMemoryForJit);
|
||||
|
||||
MemoryRegion memoryRegion = (MemoryRegion)((npdm.Acid.Value.Flags >> 2) & 0xf);
|
||||
|
||||
|
@ -2,9 +2,12 @@ using LibHac;
|
||||
using LibHac.Account;
|
||||
using LibHac.Common;
|
||||
using LibHac.Fs;
|
||||
using LibHac.FsSystem;
|
||||
using LibHac.Ncm;
|
||||
using LibHac.Ns;
|
||||
using Ryujinx.Common;
|
||||
using Ryujinx.Common.Logging;
|
||||
using Ryujinx.HLE.Exceptions;
|
||||
using Ryujinx.HLE.HOS.Ipc;
|
||||
using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
using Ryujinx.HLE.HOS.Kernel.Memory;
|
||||
@ -12,9 +15,11 @@ using Ryujinx.HLE.HOS.Kernel.Threading;
|
||||
using Ryujinx.HLE.HOS.Services.Am.AppletAE.Storage;
|
||||
using Ryujinx.HLE.HOS.Services.Am.AppletOE.ApplicationProxyService.ApplicationProxy.Types;
|
||||
using Ryujinx.HLE.HOS.Services.Sdb.Pdm.QueryService;
|
||||
using Ryujinx.HLE.HOS.Services.Sm;
|
||||
using Ryujinx.HLE.HOS.SystemState;
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Threading;
|
||||
|
||||
using static LibHac.Fs.ApplicationSaveDataManagement;
|
||||
using AccountUid = Ryujinx.HLE.HOS.Services.Account.Acc.UserId;
|
||||
@ -37,6 +42,8 @@ namespace Ryujinx.HLE.HOS.Services.Am.AppletOE.ApplicationProxyService.Applicati
|
||||
private int _notificationStorageChannelEventHandle;
|
||||
private int _healthWarningDisappearedSystemEventHandle;
|
||||
|
||||
private int _jitLoaded;
|
||||
|
||||
private HorizonClient _horizon;
|
||||
|
||||
public IApplicationFunctions(Horizon system)
|
||||
@ -631,5 +638,31 @@ namespace Ryujinx.HLE.HOS.Services.Am.AppletOE.ApplicationProxyService.Applicati
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
[CommandHipc(1001)] // 10.0.0+
|
||||
// PrepareForJit()
|
||||
public ResultCode PrepareForJit(ServiceCtx context)
|
||||
{
|
||||
if (Interlocked.Exchange(ref _jitLoaded, 1) == 0)
|
||||
{
|
||||
string jitPath = context.Device.System.ContentManager.GetInstalledContentPath(0x010000000000003B, StorageId.BuiltInSystem, NcaContentType.Program);
|
||||
string filePath = context.Device.FileSystem.SwitchPathToSystemPath(jitPath);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(filePath))
|
||||
{
|
||||
throw new InvalidSystemResourceException($"JIT (010000000000003B) system title not found! The JIT will not work, provide the system archive to fix this error. (See https://github.com/Ryujinx/Ryujinx#requirements for more information)");
|
||||
}
|
||||
|
||||
context.Device.Application.LoadServiceNca(filePath);
|
||||
|
||||
// FIXME: Most likely not how this should be done?
|
||||
while (!context.Device.System.SmRegistry.IsServiceRegistered("jit:u"))
|
||||
{
|
||||
context.Device.System.SmRegistry.WaitForServiceRegistration();
|
||||
}
|
||||
}
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
using Concentus.Structs;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
|
||||
{
|
||||
class Decoder : IDecoder
|
||||
{
|
||||
private readonly OpusDecoder _decoder;
|
||||
|
||||
public int SampleRate => _decoder.SampleRate;
|
||||
public int ChannelsCount => _decoder.NumChannels;
|
||||
|
||||
public Decoder(int sampleRate, int channelsCount)
|
||||
{
|
||||
_decoder = new OpusDecoder(sampleRate, channelsCount);
|
||||
}
|
||||
|
||||
public int Decode(byte[] inData, int inDataOffset, int len, short[] outPcm, int outPcmOffset, int frameSize)
|
||||
{
|
||||
return _decoder.Decode(inData, inDataOffset, len, outPcm, outPcmOffset, frameSize);
|
||||
}
|
||||
|
||||
public void ResetState()
|
||||
{
|
||||
_decoder.ResetState();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
using Concentus;
|
||||
using Concentus.Enums;
|
||||
using Concentus.Structs;
|
||||
using Ryujinx.HLE.HOS.Services.Audio.Types;
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
|
||||
{
|
||||
static class DecoderCommon
|
||||
{
|
||||
private static ResultCode GetPacketNumSamples(this IDecoder decoder, out int numSamples, byte[] packet)
|
||||
{
|
||||
int result = OpusPacketInfo.GetNumSamples(packet, 0, packet.Length, decoder.SampleRate);
|
||||
|
||||
numSamples = result;
|
||||
|
||||
if (result == OpusError.OPUS_INVALID_PACKET)
|
||||
{
|
||||
return ResultCode.OpusInvalidInput;
|
||||
}
|
||||
else if (result == OpusError.OPUS_BAD_ARG)
|
||||
{
|
||||
return ResultCode.OpusInvalidInput;
|
||||
}
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
public static ResultCode DecodeInterleaved(
|
||||
this IDecoder decoder,
|
||||
bool reset,
|
||||
ReadOnlySpan<byte> input,
|
||||
out short[] outPcmData,
|
||||
ulong outputSize,
|
||||
out uint outConsumed,
|
||||
out int outSamples)
|
||||
{
|
||||
outPcmData = null;
|
||||
outConsumed = 0;
|
||||
outSamples = 0;
|
||||
|
||||
int streamSize = input.Length;
|
||||
|
||||
if (streamSize < Unsafe.SizeOf<OpusPacketHeader>())
|
||||
{
|
||||
return ResultCode.OpusInvalidInput;
|
||||
}
|
||||
|
||||
OpusPacketHeader header = OpusPacketHeader.FromSpan(input);
|
||||
int headerSize = Unsafe.SizeOf<OpusPacketHeader>();
|
||||
uint totalSize = header.length + (uint)headerSize;
|
||||
|
||||
if (totalSize > streamSize)
|
||||
{
|
||||
return ResultCode.OpusInvalidInput;
|
||||
}
|
||||
|
||||
byte[] opusData = input.Slice(headerSize, (int)header.length).ToArray();
|
||||
|
||||
ResultCode result = decoder.GetPacketNumSamples(out int numSamples, opusData);
|
||||
|
||||
if (result == ResultCode.Success)
|
||||
{
|
||||
if ((uint)numSamples * (uint)decoder.ChannelsCount * sizeof(short) > outputSize)
|
||||
{
|
||||
return ResultCode.OpusInvalidInput;
|
||||
}
|
||||
|
||||
outPcmData = new short[numSamples * decoder.ChannelsCount];
|
||||
|
||||
if (reset)
|
||||
{
|
||||
decoder.ResetState();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
outSamples = decoder.Decode(opusData, 0, opusData.Length, outPcmData, 0, outPcmData.Length / decoder.ChannelsCount);
|
||||
outConsumed = totalSize;
|
||||
}
|
||||
catch (OpusException)
|
||||
{
|
||||
// TODO: as OpusException doesn't provide us the exact error code, this is kind of inaccurate in some cases...
|
||||
return ResultCode.OpusInvalidInput;
|
||||
}
|
||||
}
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
|
||||
{
|
||||
interface IDecoder
|
||||
{
|
||||
int SampleRate { get; }
|
||||
int ChannelsCount { get; }
|
||||
|
||||
int Decode(byte[] inData, int inDataOffset, int len, short[] outPcm, int outPcmOffset, int frameSize);
|
||||
void ResetState();
|
||||
}
|
||||
}
|
@ -1,244 +1,112 @@
|
||||
using Concentus;
|
||||
using Concentus.Enums;
|
||||
using Concentus.Structs;
|
||||
using Ryujinx.HLE.HOS.Services.Audio.Types;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
|
||||
{
|
||||
class IHardwareOpusDecoder : IpcService
|
||||
{
|
||||
private int _sampleRate;
|
||||
private int _channelsCount;
|
||||
private bool _reset;
|
||||
private readonly IDecoder _decoder;
|
||||
private readonly OpusDecoderFlags _flags;
|
||||
|
||||
private OpusDecoder _decoder;
|
||||
|
||||
public IHardwareOpusDecoder(int sampleRate, int channelsCount)
|
||||
public IHardwareOpusDecoder(int sampleRate, int channelsCount, OpusDecoderFlags flags)
|
||||
{
|
||||
_sampleRate = sampleRate;
|
||||
_channelsCount = channelsCount;
|
||||
_reset = false;
|
||||
|
||||
_decoder = new OpusDecoder(sampleRate, channelsCount);
|
||||
_decoder = new Decoder(sampleRate, channelsCount);
|
||||
_flags = flags;
|
||||
}
|
||||
|
||||
private ResultCode GetPacketNumSamples(out int numSamples, byte[] packet)
|
||||
public IHardwareOpusDecoder(int sampleRate, int channelsCount, int streams, int coupledStreams, OpusDecoderFlags flags, byte[] mapping)
|
||||
{
|
||||
int result = OpusPacketInfo.GetNumSamples(_decoder, packet, 0, packet.Length);
|
||||
|
||||
numSamples = result;
|
||||
|
||||
if (result == OpusError.OPUS_INVALID_PACKET)
|
||||
{
|
||||
return ResultCode.OpusInvalidInput;
|
||||
}
|
||||
else if (result == OpusError.OPUS_BAD_ARG)
|
||||
{
|
||||
return ResultCode.OpusInvalidInput;
|
||||
}
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
private ResultCode DecodeInterleavedInternal(BinaryReader input, out short[] outPcmData, long outputSize, out uint outConsumed, out int outSamples)
|
||||
{
|
||||
outPcmData = null;
|
||||
outConsumed = 0;
|
||||
outSamples = 0;
|
||||
|
||||
long streamSize = input.BaseStream.Length;
|
||||
|
||||
if (streamSize < Marshal.SizeOf<OpusPacketHeader>())
|
||||
{
|
||||
return ResultCode.OpusInvalidInput;
|
||||
}
|
||||
|
||||
OpusPacketHeader header = OpusPacketHeader.FromStream(input);
|
||||
|
||||
uint totalSize = header.length + (uint)Marshal.SizeOf<OpusPacketHeader>();
|
||||
|
||||
if (totalSize > streamSize)
|
||||
{
|
||||
return ResultCode.OpusInvalidInput;
|
||||
}
|
||||
|
||||
byte[] opusData = input.ReadBytes((int)header.length);
|
||||
|
||||
ResultCode result = GetPacketNumSamples(out int numSamples, opusData);
|
||||
|
||||
if (result == ResultCode.Success)
|
||||
{
|
||||
if ((uint)numSamples * (uint)_channelsCount * sizeof(short) > outputSize)
|
||||
{
|
||||
return ResultCode.OpusInvalidInput;
|
||||
}
|
||||
|
||||
outPcmData = new short[numSamples * _channelsCount];
|
||||
|
||||
if (_reset)
|
||||
{
|
||||
_reset = false;
|
||||
|
||||
_decoder.ResetState();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
outSamples = _decoder.Decode(opusData, 0, opusData.Length, outPcmData, 0, outPcmData.Length / _channelsCount);
|
||||
outConsumed = totalSize;
|
||||
}
|
||||
catch (OpusException)
|
||||
{
|
||||
// TODO: as OpusException doesn't provide us the exact error code, this is kind of inaccurate in some cases...
|
||||
return ResultCode.OpusInvalidInput;
|
||||
}
|
||||
}
|
||||
|
||||
return ResultCode.Success;
|
||||
_decoder = new MultiSampleDecoder(sampleRate, channelsCount, streams, coupledStreams, mapping);
|
||||
_flags = flags;
|
||||
}
|
||||
|
||||
[CommandHipc(0)]
|
||||
// DecodeInterleaved(buffer<unknown, 5>) -> (u32, u32, buffer<unknown, 6>)
|
||||
public ResultCode DecodeInterleavedOriginal(ServiceCtx context)
|
||||
// DecodeInterleavedOld(buffer<unknown, 5>) -> (u32, u32, buffer<unknown, 6>)
|
||||
public ResultCode DecodeInterleavedOld(ServiceCtx context)
|
||||
{
|
||||
ResultCode result;
|
||||
return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset: false, withPerf: false);
|
||||
}
|
||||
|
||||
ulong inPosition = context.Request.SendBuff[0].Position;
|
||||
ulong inSize = context.Request.SendBuff[0].Size;
|
||||
ulong outputPosition = context.Request.ReceiveBuff[0].Position;
|
||||
ulong outputSize = context.Request.ReceiveBuff[0].Size;
|
||||
|
||||
byte[] buffer = new byte[inSize];
|
||||
|
||||
context.Memory.Read(inPosition, buffer);
|
||||
|
||||
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
|
||||
{
|
||||
result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples);
|
||||
|
||||
if (result == ResultCode.Success)
|
||||
{
|
||||
byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
|
||||
Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
|
||||
context.Memory.Write(outputPosition, pcmDataBytes);
|
||||
|
||||
context.ResponseData.Write(outConsumed);
|
||||
context.ResponseData.Write(outSamples);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
[CommandHipc(2)]
|
||||
// DecodeInterleavedForMultiStreamOld(buffer<unknown, 5>) -> (u32, u32, buffer<unknown, 6>)
|
||||
public ResultCode DecodeInterleavedForMultiStreamOld(ServiceCtx context)
|
||||
{
|
||||
return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset: false, withPerf: false);
|
||||
}
|
||||
|
||||
[CommandHipc(4)] // 6.0.0+
|
||||
// DecodeInterleavedWithPerfOld(buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
||||
public ResultCode DecodeInterleavedWithPerfOld(ServiceCtx context)
|
||||
{
|
||||
ResultCode result;
|
||||
return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset: false, withPerf: true);
|
||||
}
|
||||
|
||||
ulong inPosition = context.Request.SendBuff[0].Position;
|
||||
ulong inSize = context.Request.SendBuff[0].Size;
|
||||
ulong outputPosition = context.Request.ReceiveBuff[0].Position;
|
||||
ulong outputSize = context.Request.ReceiveBuff[0].Size;
|
||||
|
||||
byte[] buffer = new byte[inSize];
|
||||
|
||||
context.Memory.Read(inPosition, buffer);
|
||||
|
||||
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
|
||||
{
|
||||
result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples);
|
||||
|
||||
if (result == ResultCode.Success)
|
||||
{
|
||||
byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
|
||||
Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
|
||||
context.Memory.Write(outputPosition, pcmDataBytes);
|
||||
|
||||
context.ResponseData.Write(outConsumed);
|
||||
context.ResponseData.Write(outSamples);
|
||||
|
||||
// This is the time the DSP took to process the request, TODO: fill this.
|
||||
context.ResponseData.Write(0);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
[CommandHipc(5)] // 6.0.0+
|
||||
// DecodeInterleavedForMultiStreamWithPerfOld(buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
||||
public ResultCode DecodeInterleavedForMultiStreamWithPerfOld(ServiceCtx context)
|
||||
{
|
||||
return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset: false, withPerf: true);
|
||||
}
|
||||
|
||||
[CommandHipc(6)] // 6.0.0+
|
||||
// DecodeInterleavedOld(bool reset, buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
||||
public ResultCode DecodeInterleavedOld(ServiceCtx context)
|
||||
// DecodeInterleavedWithPerfAndResetOld(bool reset, buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
||||
public ResultCode DecodeInterleavedWithPerfAndResetOld(ServiceCtx context)
|
||||
{
|
||||
ResultCode result;
|
||||
bool reset = context.RequestData.ReadBoolean();
|
||||
|
||||
_reset = context.RequestData.ReadBoolean();
|
||||
return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset, withPerf: true);
|
||||
}
|
||||
|
||||
ulong inPosition = context.Request.SendBuff[0].Position;
|
||||
ulong inSize = context.Request.SendBuff[0].Size;
|
||||
ulong outputPosition = context.Request.ReceiveBuff[0].Position;
|
||||
ulong outputSize = context.Request.ReceiveBuff[0].Size;
|
||||
[CommandHipc(7)] // 6.0.0+
|
||||
// DecodeInterleavedForMultiStreamWithPerfAndResetOld(bool reset, buffer<unknown, 5>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
||||
public ResultCode DecodeInterleavedForMultiStreamWithPerfAndResetOld(ServiceCtx context)
|
||||
{
|
||||
bool reset = context.RequestData.ReadBoolean();
|
||||
|
||||
byte[] buffer = new byte[inSize];
|
||||
|
||||
context.Memory.Read(inPosition, buffer);
|
||||
|
||||
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
|
||||
{
|
||||
result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples);
|
||||
|
||||
if (result == ResultCode.Success)
|
||||
{
|
||||
byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
|
||||
Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
|
||||
context.Memory.Write(outputPosition, pcmDataBytes);
|
||||
|
||||
context.ResponseData.Write(outConsumed);
|
||||
context.ResponseData.Write(outSamples);
|
||||
|
||||
// This is the time the DSP took to process the request, TODO: fill this.
|
||||
context.ResponseData.Write(0);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return DecodeInterleavedInternal(context, OpusDecoderFlags.None, reset, withPerf: true);
|
||||
}
|
||||
|
||||
[CommandHipc(8)] // 7.0.0+
|
||||
// DecodeInterleaved(bool reset, buffer<unknown, 0x45>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
||||
public ResultCode DecodeInterleaved(ServiceCtx context)
|
||||
{
|
||||
ResultCode result;
|
||||
bool reset = context.RequestData.ReadBoolean();
|
||||
|
||||
_reset = context.RequestData.ReadBoolean();
|
||||
return DecodeInterleavedInternal(context, _flags, reset, withPerf: true);
|
||||
}
|
||||
|
||||
[CommandHipc(9)] // 7.0.0+
|
||||
// DecodeInterleavedForMultiStream(bool reset, buffer<unknown, 0x45>) -> (u32, u32, u64, buffer<unknown, 0x46>)
|
||||
public ResultCode DecodeInterleavedForMultiStream(ServiceCtx context)
|
||||
{
|
||||
bool reset = context.RequestData.ReadBoolean();
|
||||
|
||||
return DecodeInterleavedInternal(context, _flags, reset, withPerf: true);
|
||||
}
|
||||
|
||||
private ResultCode DecodeInterleavedInternal(ServiceCtx context, OpusDecoderFlags flags, bool reset, bool withPerf)
|
||||
{
|
||||
ulong inPosition = context.Request.SendBuff[0].Position;
|
||||
ulong inSize = context.Request.SendBuff[0].Size;
|
||||
ulong outputPosition = context.Request.ReceiveBuff[0].Position;
|
||||
ulong outputSize = context.Request.ReceiveBuff[0].Size;
|
||||
|
||||
byte[] buffer = new byte[inSize];
|
||||
ReadOnlySpan<byte> input = context.Memory.GetSpan(inPosition, (int)inSize);
|
||||
|
||||
context.Memory.Read(inPosition, buffer);
|
||||
ResultCode result = _decoder.DecodeInterleaved(reset, input, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples);
|
||||
|
||||
using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer)))
|
||||
if (result == ResultCode.Success)
|
||||
{
|
||||
result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, (long)outputSize, out uint outConsumed, out int outSamples);
|
||||
context.Memory.Write(outputPosition, MemoryMarshal.Cast<short, byte>(outPcmData.AsSpan()));
|
||||
|
||||
if (result == ResultCode.Success)
|
||||
context.ResponseData.Write(outConsumed);
|
||||
context.ResponseData.Write(outSamples);
|
||||
|
||||
if (withPerf)
|
||||
{
|
||||
byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)];
|
||||
Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length);
|
||||
context.Memory.Write(outputPosition, pcmDataBytes);
|
||||
|
||||
context.ResponseData.Write(outConsumed);
|
||||
context.ResponseData.Write(outSamples);
|
||||
|
||||
// This is the time the DSP took to process the request, TODO: fill this.
|
||||
context.ResponseData.Write(0);
|
||||
context.ResponseData.Write(0UL);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,28 @@
|
||||
using Concentus.Structs;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager
|
||||
{
|
||||
class MultiSampleDecoder : IDecoder
|
||||
{
|
||||
private readonly OpusMSDecoder _decoder;
|
||||
|
||||
public int SampleRate => _decoder.SampleRate;
|
||||
public int ChannelsCount { get; }
|
||||
|
||||
public MultiSampleDecoder(int sampleRate, int channelsCount, int streams, int coupledStreams, byte[] mapping)
|
||||
{
|
||||
ChannelsCount = channelsCount;
|
||||
_decoder = new OpusMSDecoder(sampleRate, channelsCount, streams, coupledStreams, mapping);
|
||||
}
|
||||
|
||||
public int Decode(byte[] inData, int inDataOffset, int len, short[] outPcm, int outPcmOffset, int frameSize)
|
||||
{
|
||||
return _decoder.DecodeMultistream(inData, inDataOffset, len, outPcm, outPcmOffset, frameSize, 0);
|
||||
}
|
||||
|
||||
public void ResetState()
|
||||
{
|
||||
_decoder.ResetState();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
using Ryujinx.Common;
|
||||
using Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager;
|
||||
using Ryujinx.HLE.HOS.Services.Audio.Types;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Audio
|
||||
{
|
||||
@ -16,7 +17,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio
|
||||
int sampleRate = context.RequestData.ReadInt32();
|
||||
int channelsCount = context.RequestData.ReadInt32();
|
||||
|
||||
MakeObject(context, new IHardwareOpusDecoder(sampleRate, channelsCount));
|
||||
MakeObject(context, new IHardwareOpusDecoder(sampleRate, channelsCount, OpusDecoderFlags.None));
|
||||
|
||||
// Close transfer memory immediately as we don't use it.
|
||||
context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);
|
||||
@ -28,11 +29,50 @@ namespace Ryujinx.HLE.HOS.Services.Audio
|
||||
// GetWorkBufferSize(bytes<8, 4>) -> u32
|
||||
public ResultCode GetWorkBufferSize(ServiceCtx context)
|
||||
{
|
||||
// NOTE: The sample rate is ignored because it is fixed to 48KHz.
|
||||
int sampleRate = context.RequestData.ReadInt32();
|
||||
int channelsCount = context.RequestData.ReadInt32();
|
||||
|
||||
context.ResponseData.Write(GetOpusDecoderSize(channelsCount));
|
||||
int opusDecoderSize = GetOpusDecoderSize(channelsCount);
|
||||
|
||||
int frameSize = BitUtils.AlignUp(channelsCount * 1920 / (48000 / sampleRate), 64);
|
||||
int totalSize = opusDecoderSize + 1536 + frameSize;
|
||||
|
||||
context.ResponseData.Write(totalSize);
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
[CommandHipc(2)] // 3.0.0+
|
||||
// InitializeForMultiStream(u32, handle<copy>, buffer<unknown<0x110>, 0x19>) -> object<nn::codec::detail::IHardwareOpusDecoder>
|
||||
public ResultCode InitializeForMultiStream(ServiceCtx context)
|
||||
{
|
||||
ulong parametersAddress = context.Request.PtrBuff[0].Position;
|
||||
|
||||
OpusMultiStreamParameters parameters = context.Memory.Read<OpusMultiStreamParameters>(parametersAddress);
|
||||
|
||||
MakeObject(context, new IHardwareOpusDecoder(parameters.SampleRate, parameters.ChannelsCount, OpusDecoderFlags.None));
|
||||
|
||||
// Close transfer memory immediately as we don't use it.
|
||||
context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
[CommandHipc(3)] // 3.0.0+
|
||||
// GetWorkBufferSizeForMultiStream(buffer<unknown<0x110>, 0x19>) -> u32
|
||||
public ResultCode GetWorkBufferSizeForMultiStream(ServiceCtx context)
|
||||
{
|
||||
ulong parametersAddress = context.Request.PtrBuff[0].Position;
|
||||
|
||||
OpusMultiStreamParameters parameters = context.Memory.Read<OpusMultiStreamParameters>(parametersAddress);
|
||||
|
||||
int opusDecoderSize = GetOpusMultistreamDecoderSize(parameters.NumberOfStreams, parameters.NumberOfStereoStreams);
|
||||
|
||||
int streamSize = BitUtils.AlignUp(parameters.NumberOfStreams * 1500, 64);
|
||||
int frameSize = BitUtils.AlignUp(parameters.ChannelsCount * 1920 / (48000 / parameters.SampleRate), 64);
|
||||
int totalSize = opusDecoderSize + streamSize + frameSize;
|
||||
|
||||
context.ResponseData.Write(totalSize);
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
@ -44,7 +84,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio
|
||||
OpusParametersEx parameters = context.RequestData.ReadStruct<OpusParametersEx>();
|
||||
|
||||
// UseLargeFrameSize can be ignored due to not relying on fixed size buffers for storing the decoded result.
|
||||
MakeObject(context, new IHardwareOpusDecoder(parameters.SampleRate, parameters.ChannelCount));
|
||||
MakeObject(context, new IHardwareOpusDecoder(parameters.SampleRate, parameters.ChannelsCount, parameters.Flags));
|
||||
|
||||
// Close transfer memory immediately as we don't use it.
|
||||
context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);
|
||||
@ -58,15 +98,84 @@ namespace Ryujinx.HLE.HOS.Services.Audio
|
||||
{
|
||||
OpusParametersEx parameters = context.RequestData.ReadStruct<OpusParametersEx>();
|
||||
|
||||
// NOTE: The sample rate is ignored because it is fixed to 48KHz.
|
||||
context.ResponseData.Write(GetOpusDecoderSize(parameters.ChannelCount));
|
||||
int opusDecoderSize = GetOpusDecoderSize(parameters.ChannelsCount);
|
||||
|
||||
int frameSizeMono48KHz = parameters.Flags.HasFlag(OpusDecoderFlags.LargeFrameSize) ? 5760 : 1920;
|
||||
int frameSize = BitUtils.AlignUp(parameters.ChannelsCount * frameSizeMono48KHz / (48000 / parameters.SampleRate), 64);
|
||||
int totalSize = opusDecoderSize + 1536 + frameSize;
|
||||
|
||||
context.ResponseData.Write(totalSize);
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
[CommandHipc(6)] // 12.0.0+
|
||||
// InitializeForMultiStreamEx(u32, handle<copy>, buffer<unknown<0x118>, 0x19>) -> object<nn::codec::detail::IHardwareOpusDecoder>
|
||||
public ResultCode InitializeForMultiStreamEx(ServiceCtx context)
|
||||
{
|
||||
ulong parametersAddress = context.Request.PtrBuff[0].Position;
|
||||
|
||||
OpusMultiStreamParametersEx parameters = context.Memory.Read<OpusMultiStreamParametersEx>(parametersAddress);
|
||||
|
||||
byte[] mappings = MemoryMarshal.Cast<uint, byte>(parameters.ChannelMappings.ToSpan()).ToArray();
|
||||
|
||||
// UseLargeFrameSize can be ignored due to not relying on fixed size buffers for storing the decoded result.
|
||||
MakeObject(context, new IHardwareOpusDecoder(
|
||||
parameters.SampleRate,
|
||||
parameters.ChannelsCount,
|
||||
parameters.NumberOfStreams,
|
||||
parameters.NumberOfStereoStreams,
|
||||
parameters.Flags,
|
||||
mappings));
|
||||
|
||||
// Close transfer memory immediately as we don't use it.
|
||||
context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
[CommandHipc(7)] // 12.0.0+
|
||||
// GetWorkBufferSizeForMultiStreamEx(buffer<unknown<0x118>, 0x19>) -> u32
|
||||
public ResultCode GetWorkBufferSizeForMultiStreamEx(ServiceCtx context)
|
||||
{
|
||||
ulong parametersAddress = context.Request.PtrBuff[0].Position;
|
||||
|
||||
OpusMultiStreamParametersEx parameters = context.Memory.Read<OpusMultiStreamParametersEx>(parametersAddress);
|
||||
|
||||
int opusDecoderSize = GetOpusMultistreamDecoderSize(parameters.NumberOfStreams, parameters.NumberOfStereoStreams);
|
||||
|
||||
int frameSizeMono48KHz = parameters.Flags.HasFlag(OpusDecoderFlags.LargeFrameSize) ? 5760 : 1920;
|
||||
int streamSize = BitUtils.AlignUp(parameters.NumberOfStreams * 1500, 64);
|
||||
int frameSize = BitUtils.AlignUp(parameters.ChannelsCount * frameSizeMono48KHz / (48000 / parameters.SampleRate), 64);
|
||||
int totalSize = opusDecoderSize + streamSize + frameSize;
|
||||
|
||||
context.ResponseData.Write(totalSize);
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
private static int GetOpusMultistreamDecoderSize(int streams, int coupledStreams)
|
||||
{
|
||||
if (streams < 1 || coupledStreams > streams || coupledStreams < 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int coupledSize = GetOpusDecoderSize(2);
|
||||
int monoSize = GetOpusDecoderSize(1);
|
||||
|
||||
return Align4(monoSize - GetOpusDecoderAllocSize(1)) * (streams - coupledStreams) +
|
||||
Align4(coupledSize - GetOpusDecoderAllocSize(2)) * coupledStreams + 0xb90c;
|
||||
}
|
||||
|
||||
private static int Align4(int value)
|
||||
{
|
||||
return BitUtils.AlignUp(value, 4);
|
||||
}
|
||||
|
||||
private static int GetOpusDecoderSize(int channelsCount)
|
||||
{
|
||||
const int silkDecoderSize = 0x2198;
|
||||
const int SilkDecoderSize = 0x2160;
|
||||
|
||||
if (channelsCount < 1 || channelsCount > 2)
|
||||
{
|
||||
@ -74,24 +183,23 @@ namespace Ryujinx.HLE.HOS.Services.Audio
|
||||
}
|
||||
|
||||
int celtDecoderSize = GetCeltDecoderSize(channelsCount);
|
||||
int opusDecoderSize = GetOpusDecoderAllocSize(channelsCount) | 0x4c;
|
||||
|
||||
int opusDecoderSize = (channelsCount * 0x800 + 0x4807) & -0x800 | 0x50;
|
||||
return opusDecoderSize + SilkDecoderSize + celtDecoderSize;
|
||||
}
|
||||
|
||||
return opusDecoderSize + silkDecoderSize + celtDecoderSize;
|
||||
private static int GetOpusDecoderAllocSize(int channelsCount)
|
||||
{
|
||||
return (channelsCount * 0x800 + 0x4803) & -0x800;
|
||||
}
|
||||
|
||||
private static int GetCeltDecoderSize(int channelsCount)
|
||||
{
|
||||
const int decodeBufferSize = 0x2030;
|
||||
const int celtDecoderSize = 0x58;
|
||||
const int celtSigSize = 0x4;
|
||||
const int overlap = 120;
|
||||
const int eBandsCount = 21;
|
||||
const int DecodeBufferSize = 0x2030;
|
||||
const int Overlap = 120;
|
||||
const int EBandsCount = 21;
|
||||
|
||||
return (decodeBufferSize + overlap * 4) * channelsCount +
|
||||
eBandsCount * 16 +
|
||||
celtDecoderSize +
|
||||
celtSigSize;
|
||||
return (DecodeBufferSize + Overlap * 4) * channelsCount + EBandsCount * 16 + 0x50;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
using Ryujinx.Common.Memory;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Audio.Types
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential, Size = 0x110)]
|
||||
struct OpusMultiStreamParameters
|
||||
{
|
||||
public int SampleRate;
|
||||
public int ChannelsCount;
|
||||
public int NumberOfStreams;
|
||||
public int NumberOfStereoStreams;
|
||||
public Array64<uint> ChannelMappings;
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
using Ryujinx.Common.Memory;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Audio.Types
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential, Size = 0x118)]
|
||||
struct OpusMultiStreamParametersEx
|
||||
{
|
||||
public int SampleRate;
|
||||
public int ChannelsCount;
|
||||
public int NumberOfStreams;
|
||||
public int NumberOfStereoStreams;
|
||||
public OpusDecoderFlags Flags;
|
||||
|
||||
Array4<byte> Padding1;
|
||||
|
||||
public Array64<uint> ChannelMappings;
|
||||
}
|
||||
}
|
@ -12,9 +12,9 @@ namespace Ryujinx.HLE.HOS.Services.Audio.Types
|
||||
public uint length;
|
||||
public uint finalRange;
|
||||
|
||||
public static OpusPacketHeader FromStream(BinaryReader reader)
|
||||
public static OpusPacketHeader FromSpan(ReadOnlySpan<byte> data)
|
||||
{
|
||||
OpusPacketHeader header = reader.ReadStruct<OpusPacketHeader>();
|
||||
OpusPacketHeader header = MemoryMarshal.Cast<byte, OpusPacketHeader>(data)[0];
|
||||
|
||||
header.length = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(header.length) : header.length;
|
||||
header.finalRange = BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness(header.finalRange) : header.finalRange;
|
||||
|
@ -7,8 +7,8 @@ namespace Ryujinx.HLE.HOS.Services.Audio.Types
|
||||
struct OpusParametersEx
|
||||
{
|
||||
public int SampleRate;
|
||||
public int ChannelCount;
|
||||
public OpusDecoderFlags UseLargeFrameSize;
|
||||
public int ChannelsCount;
|
||||
public OpusDecoderFlags Flags;
|
||||
|
||||
Array4<byte> Padding1;
|
||||
}
|
||||
|
@ -1,8 +1,31 @@
|
||||
namespace Ryujinx.HLE.HOS.Services.Pm
|
||||
using Ryujinx.HLE.HOS.Ipc;
|
||||
using Ryujinx.HLE.HOS.Kernel;
|
||||
using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Pm
|
||||
{
|
||||
[Service("pm:dmnt")]
|
||||
class IDebugMonitorInterface : IpcService
|
||||
{
|
||||
public IDebugMonitorInterface(ServiceCtx context) { }
|
||||
|
||||
[CommandHipc(65000)]
|
||||
// AtmosphereGetProcessInfo(os::ProcessId process_id) -> sf::OutCopyHandle out_process_handle, sf::Out<ncm::ProgramLocation> out_loc, sf::Out<cfg::OverrideStatus> out_status
|
||||
public ResultCode GetProcessInfo(ServiceCtx context)
|
||||
{
|
||||
ulong pid = context.RequestData.ReadUInt64();
|
||||
|
||||
KProcess process = KernelStatic.GetProcessByPid(pid);
|
||||
|
||||
if (context.Process.HandleTable.GenerateHandle(process, out int processHandle) != KernelResult.Success)
|
||||
{
|
||||
throw new System.Exception("Out of handles!");
|
||||
}
|
||||
|
||||
context.Response.HandleDesc = IpcHandleDesc.MakeCopy(processHandle);
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
using Ryujinx.HLE.HOS.Kernel.Memory;
|
||||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
using Ryujinx.HLE.Loaders.Executables;
|
||||
using Ryujinx.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
@ -29,6 +30,7 @@ namespace Ryujinx.HLE.HOS.Services.Ro
|
||||
private List<NroInfo> _nroInfos;
|
||||
|
||||
private KProcess _owner;
|
||||
private IVirtualMemoryManager _ownerMm;
|
||||
|
||||
private static Random _random = new Random();
|
||||
|
||||
@ -37,6 +39,7 @@ namespace Ryujinx.HLE.HOS.Services.Ro
|
||||
_nrrInfos = new List<NrrInfo>(MaxNrr);
|
||||
_nroInfos = new List<NroInfo>(MaxNro);
|
||||
_owner = null;
|
||||
_ownerMm = null;
|
||||
}
|
||||
|
||||
private ResultCode ParseNrr(out NrrInfo nrrInfo, ServiceCtx context, ulong nrrAddress, ulong nrrSize)
|
||||
@ -563,12 +566,28 @@ namespace Ryujinx.HLE.HOS.Services.Ro
|
||||
return ResultCode.InvalidSession;
|
||||
}
|
||||
|
||||
_owner = context.Process.HandleTable.GetKProcess(context.Request.HandleDesc.ToCopy[0]);
|
||||
context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);
|
||||
int processHandle = context.Request.HandleDesc.ToCopy[0];
|
||||
_owner = context.Process.HandleTable.GetKProcess(processHandle);
|
||||
_ownerMm = _owner?.CpuMemory;
|
||||
context.Device.System.KernelContext.Syscall.CloseHandle(processHandle);
|
||||
|
||||
if (_ownerMm is IRefCounted rc)
|
||||
{
|
||||
rc.IncrementReferenceCount();
|
||||
}
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
[CommandHipc(10)]
|
||||
// LoadNrr2(u64, u64, u64, pid)
|
||||
public ResultCode LoadNrr2(ServiceCtx context)
|
||||
{
|
||||
context.Device.System.KernelContext.Syscall.CloseHandle(context.Request.HandleDesc.ToCopy[0]);
|
||||
|
||||
return LoadNrr(context);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool isDisposing)
|
||||
{
|
||||
if (isDisposing)
|
||||
@ -579,6 +598,11 @@ namespace Ryujinx.HLE.HOS.Services.Ro
|
||||
}
|
||||
|
||||
_nroInfos.Clear();
|
||||
|
||||
if (_ownerMm is IRefCounted rc)
|
||||
{
|
||||
rc.DecrementReferenceCount();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
using Ryujinx.HLE.HOS.Kernel.Ipc;
|
||||
using Ryujinx.HLE.HOS.Kernel.Process;
|
||||
using Ryujinx.HLE.HOS.Kernel.Threading;
|
||||
using Ryujinx.HLE.HOS.Services.Sm;
|
||||
using System;
|
||||
using System.Buffers.Binary;
|
||||
using System.Collections.Generic;
|
||||
|
@ -222,7 +222,7 @@ namespace Ryujinx.HLE.HOS.Services.Settings
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
[CommandHipc(60)]
|
||||
[CommandHipc(60)]
|
||||
// IsUserSystemClockAutomaticCorrectionEnabled() -> bool
|
||||
public ResultCode IsUserSystemClockAutomaticCorrectionEnabled(ServiceCtx context)
|
||||
{
|
||||
@ -234,6 +234,17 @@ namespace Ryujinx.HLE.HOS.Services.Settings
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
[CommandHipc(62)]
|
||||
// GetDebugModeFlag() -> bool
|
||||
public ResultCode GetDebugModeFlag(ServiceCtx context)
|
||||
{
|
||||
context.ResponseData.Write(false);
|
||||
|
||||
Logger.Stub?.PrintStub(LogClass.ServiceSet);
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
[CommandHipc(77)]
|
||||
// GetDeviceNickName() -> buffer<nn::settings::system::DeviceNickName, 0x16>
|
||||
public ResultCode GetDeviceNickName(ServiceCtx context)
|
||||
|
@ -1,11 +1,9 @@
|
||||
using Ryujinx.Common.Logging;
|
||||
using Ryujinx.HLE.Exceptions;
|
||||
using Ryujinx.HLE.HOS.Ipc;
|
||||
using Ryujinx.HLE.HOS.Kernel;
|
||||
using Ryujinx.HLE.HOS.Kernel.Common;
|
||||
using Ryujinx.HLE.HOS.Kernel.Ipc;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@ -17,21 +15,19 @@ namespace Ryujinx.HLE.HOS.Services.Sm
|
||||
{
|
||||
private static Dictionary<string, Type> _services;
|
||||
|
||||
private static readonly ConcurrentDictionary<string, KPort> _registeredServices;
|
||||
|
||||
private readonly SmRegistry _registry;
|
||||
private readonly ServerBase _commonServer;
|
||||
|
||||
private bool _isInitialized;
|
||||
|
||||
public IUserInterface(KernelContext context)
|
||||
public IUserInterface(KernelContext context, SmRegistry registry)
|
||||
{
|
||||
_commonServer = new ServerBase(context, "CommonServer");
|
||||
_registry = registry;
|
||||
}
|
||||
|
||||
static IUserInterface()
|
||||
{
|
||||
_registeredServices = new ConcurrentDictionary<string, KPort>();
|
||||
|
||||
_services = Assembly.GetExecutingAssembly().GetTypes()
|
||||
.SelectMany(type => type.GetCustomAttributes(typeof(ServiceAttribute), true)
|
||||
.Select(service => (((ServiceAttribute)service).Name, type)))
|
||||
@ -74,7 +70,7 @@ namespace Ryujinx.HLE.HOS.Services.Sm
|
||||
|
||||
KSession session = new KSession(context.Device.System.KernelContext);
|
||||
|
||||
if (_registeredServices.TryGetValue(name, out KPort port))
|
||||
if (_registry.TryGetService(name, out KPort port))
|
||||
{
|
||||
KernelResult result = port.EnqueueIncomingSession(session.ServerSession);
|
||||
|
||||
@ -82,6 +78,15 @@ namespace Ryujinx.HLE.HOS.Services.Sm
|
||||
{
|
||||
throw new InvalidOperationException($"Session enqueue on port returned error \"{result}\".");
|
||||
}
|
||||
|
||||
if (context.Process.HandleTable.GenerateHandle(session.ClientSession, out int handle) != KernelResult.Success)
|
||||
{
|
||||
throw new InvalidOperationException("Out of handles!");
|
||||
}
|
||||
|
||||
session.ClientSession.DecrementReferenceCount();
|
||||
|
||||
context.Response.HandleDesc = IpcHandleDesc.MakeMove(handle);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -107,18 +112,18 @@ namespace Ryujinx.HLE.HOS.Services.Sm
|
||||
throw new NotImplementedException(name);
|
||||
}
|
||||
}
|
||||
|
||||
if (context.Process.HandleTable.GenerateHandle(session.ClientSession, out int handle) != KernelResult.Success)
|
||||
{
|
||||
throw new InvalidOperationException("Out of handles!");
|
||||
}
|
||||
|
||||
session.ServerSession.DecrementReferenceCount();
|
||||
session.ClientSession.DecrementReferenceCount();
|
||||
|
||||
context.Response.HandleDesc = IpcHandleDesc.MakeMove(handle);
|
||||
}
|
||||
|
||||
if (context.Process.HandleTable.GenerateHandle(session.ClientSession, out int handle) != KernelResult.Success)
|
||||
{
|
||||
throw new InvalidOperationException("Out of handles!");
|
||||
}
|
||||
|
||||
session.ServerSession.DecrementReferenceCount();
|
||||
session.ClientSession.DecrementReferenceCount();
|
||||
|
||||
context.Response.HandleDesc = IpcHandleDesc.MakeMove(handle);
|
||||
|
||||
return ResultCode.Success;
|
||||
}
|
||||
|
||||
@ -179,7 +184,7 @@ namespace Ryujinx.HLE.HOS.Services.Sm
|
||||
|
||||
KPort port = new KPort(context.Device.System.KernelContext, maxSessions, isLight, 0);
|
||||
|
||||
if (!_registeredServices.TryAdd(name, port))
|
||||
if (!_registry.TryRegister(name, port))
|
||||
{
|
||||
return ResultCode.AlreadyRegistered;
|
||||
}
|
||||
@ -219,7 +224,7 @@ namespace Ryujinx.HLE.HOS.Services.Sm
|
||||
return ResultCode.InvalidName;
|
||||
}
|
||||
|
||||
if (!_registeredServices.TryRemove(name, out _))
|
||||
if (!_registry.Unregister(name))
|
||||
{
|
||||
return ResultCode.NotRegistered;
|
||||
}
|
||||
|
49
Ryujinx.HLE/HOS/Services/Sm/SmRegistry.cs
Normal file
49
Ryujinx.HLE/HOS/Services/Sm/SmRegistry.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using Ryujinx.HLE.HOS.Kernel.Ipc;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading;
|
||||
|
||||
namespace Ryujinx.HLE.HOS.Services.Sm
|
||||
{
|
||||
class SmRegistry
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, KPort> _registeredServices;
|
||||
private readonly AutoResetEvent _serviceRegistrationEvent;
|
||||
|
||||
public SmRegistry()
|
||||
{
|
||||
_registeredServices = new ConcurrentDictionary<string, KPort>();
|
||||
_serviceRegistrationEvent = new AutoResetEvent(false);
|
||||
}
|
||||
|
||||
public bool TryGetService(string name, out KPort port)
|
||||
{
|
||||
return _registeredServices.TryGetValue(name, out port);
|
||||
}
|
||||
|
||||
public bool TryRegister(string name, KPort port)
|
||||
{
|
||||
if (_registeredServices.TryAdd(name, port))
|
||||
{
|
||||
_serviceRegistrationEvent.Set();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Unregister(string name)
|
||||
{
|
||||
return _registeredServices.TryRemove(name, out _);
|
||||
}
|
||||
|
||||
public bool IsServiceRegistered(string name)
|
||||
{
|
||||
return _registeredServices.TryGetValue(name, out _);
|
||||
}
|
||||
|
||||
public void WaitForServiceRegistration()
|
||||
{
|
||||
_serviceRegistrationEvent.WaitOne();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
using Ryujinx.Audio.Backends.CompatLayer;
|
||||
using Ryujinx.Audio.Integration;
|
||||
using Ryujinx.Common.Configuration;
|
||||
using Ryujinx.Graphics.Gpu;
|
||||
using Ryujinx.HLE.FileSystem;
|
||||
using Ryujinx.HLE.HOS;
|
||||
@ -48,8 +49,12 @@ namespace Ryujinx.HLE
|
||||
FileSystem = Configuration.VirtualFileSystem;
|
||||
UiHandler = Configuration.HostUiHandler;
|
||||
|
||||
MemoryAllocationFlags memoryAllocationFlags = configuration.MemoryManagerMode == MemoryManagerMode.SoftwarePageTable
|
||||
? MemoryAllocationFlags.Reserve
|
||||
: MemoryAllocationFlags.Reserve | MemoryAllocationFlags.Mirrorable;
|
||||
|
||||
AudioDeviceDriver = new CompatLayerHardwareDeviceDriver(Configuration.AudioDeviceDriver);
|
||||
Memory = new MemoryBlock(Configuration.MemoryConfiguration.ToDramSize(), MemoryAllocationFlags.Reserve);
|
||||
Memory = new MemoryBlock(Configuration.MemoryConfiguration.ToDramSize(), memoryAllocationFlags);
|
||||
Gpu = new GpuContext(Configuration.GpuRenderer);
|
||||
System = new Horizon(this);
|
||||
Statistics = new PerformanceStatistics();
|
||||
|
@ -302,7 +302,7 @@ namespace Ryujinx.Input.HLE
|
||||
Vector3 gyroscope = _gamepad.GetMotionData(MotionInputId.Gyroscope);
|
||||
|
||||
accelerometer = new Vector3(accelerometer.X, -accelerometer.Z, accelerometer.Y);
|
||||
gyroscope = new Vector3(gyroscope.X, gyroscope.Z, gyroscope.Y);
|
||||
gyroscope = new Vector3(gyroscope.X, -gyroscope.Z, gyroscope.Y);
|
||||
|
||||
_leftMotionInput.Update(accelerometer, gyroscope, (ulong)PerformanceCounter.ElapsedNanoseconds / 1000, controllerConfig.Motion.Sensitivity, (float)controllerConfig.Motion.GyroDeadzone);
|
||||
|
||||
|
@ -14,7 +14,7 @@ namespace Ryujinx.Memory.Tests
|
||||
{
|
||||
}
|
||||
|
||||
public void Map(ulong va, nuint hostAddress, ulong size)
|
||||
public void Map(ulong va, ulong pa, ulong size)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
@ -59,9 +59,9 @@ namespace Ryujinx.Memory.Tests
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
IEnumerable<HostMemoryRange> IVirtualMemoryManager.GetPhysicalRegions(ulong va, ulong size)
|
||||
IEnumerable<MemoryRange> IVirtualMemoryManager.GetPhysicalRegions(ulong va, ulong size)
|
||||
{
|
||||
return NoMappings ? new HostMemoryRange[0] : new HostMemoryRange[] { new HostMemoryRange((nuint)va, size) };
|
||||
return NoMappings ? new MemoryRange[0] : new MemoryRange[] { new MemoryRange(va, size) };
|
||||
}
|
||||
|
||||
public bool IsMapped(ulong va)
|
||||
|
@ -1,5 +1,4 @@
|
||||
using NUnit.Framework;
|
||||
using Ryujinx.Memory;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
@ -38,5 +37,48 @@ namespace Ryujinx.Memory.Tests
|
||||
|
||||
Assert.AreEqual(Marshal.ReadInt32(_memoryBlock.Pointer, 0x2040), 0xbadc0de);
|
||||
}
|
||||
|
||||
[Test, Explicit]
|
||||
public void Test_Alias()
|
||||
{
|
||||
using MemoryBlock backing = new MemoryBlock(0x10000, MemoryAllocationFlags.Mirrorable);
|
||||
using MemoryBlock toAlias = new MemoryBlock(0x10000, MemoryAllocationFlags.Reserve | MemoryAllocationFlags.ViewCompatible);
|
||||
|
||||
toAlias.MapView(backing, 0x1000, 0, 0x4000);
|
||||
toAlias.UnmapView(backing, 0x3000, 0x1000);
|
||||
|
||||
toAlias.Write(0, 0xbadc0de);
|
||||
Assert.AreEqual(Marshal.ReadInt32(backing.Pointer, 0x1000), 0xbadc0de);
|
||||
}
|
||||
|
||||
[Test, Explicit]
|
||||
public void Test_AliasRandom()
|
||||
{
|
||||
using MemoryBlock backing = new MemoryBlock(0x80000, MemoryAllocationFlags.Mirrorable);
|
||||
using MemoryBlock toAlias = new MemoryBlock(0x80000, MemoryAllocationFlags.Reserve | MemoryAllocationFlags.ViewCompatible);
|
||||
|
||||
Random rng = new Random(123);
|
||||
|
||||
for (int i = 0; i < 20000; i++)
|
||||
{
|
||||
int srcPage = rng.Next(0, 64);
|
||||
int dstPage = rng.Next(0, 64);
|
||||
int pages = rng.Next(1, 65);
|
||||
|
||||
if ((rng.Next() & 1) != 0)
|
||||
{
|
||||
toAlias.MapView(backing, (ulong)srcPage << 12, (ulong)dstPage << 12, (ulong)pages << 12);
|
||||
|
||||
int offset = rng.Next(0, 0x1000 - sizeof(int));
|
||||
|
||||
toAlias.Write((ulong)((dstPage << 12) + offset), 0xbadc0de);
|
||||
Assert.AreEqual(Marshal.ReadInt32(backing.Pointer, (srcPage << 12) + offset), 0xbadc0de);
|
||||
}
|
||||
else
|
||||
{
|
||||
toAlias.UnmapView(backing, (ulong)dstPage << 12, (ulong)pages << 12);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -13,9 +13,9 @@ namespace Ryujinx.Memory
|
||||
/// </summary>
|
||||
public sealed class AddressSpaceManager : IVirtualMemoryManager, IWritableBlock
|
||||
{
|
||||
public const int PageBits = PageTable<nuint>.PageBits;
|
||||
public const int PageSize = PageTable<nuint>.PageSize;
|
||||
public const int PageMask = PageTable<nuint>.PageMask;
|
||||
public const int PageBits = PageTable<ulong>.PageBits;
|
||||
public const int PageSize = PageTable<ulong>.PageSize;
|
||||
public const int PageMask = PageTable<ulong>.PageMask;
|
||||
|
||||
/// <summary>
|
||||
/// Address space width in bits.
|
||||
@ -24,14 +24,15 @@ namespace Ryujinx.Memory
|
||||
|
||||
private readonly ulong _addressSpaceSize;
|
||||
|
||||
private readonly PageTable<nuint> _pageTable;
|
||||
private readonly MemoryBlock _backingMemory;
|
||||
private readonly PageTable<ulong> _pageTable;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the memory manager.
|
||||
/// </summary>
|
||||
/// <param name="backingMemory">Physical backing memory where virtual memory will be mapped to</param>
|
||||
/// <param name="addressSpaceSize">Size of the address space</param>
|
||||
public AddressSpaceManager(ulong addressSpaceSize)
|
||||
public AddressSpaceManager(MemoryBlock backingMemory, ulong addressSpaceSize)
|
||||
{
|
||||
ulong asSize = PageSize;
|
||||
int asBits = PageBits;
|
||||
@ -44,37 +45,26 @@ namespace Ryujinx.Memory
|
||||
|
||||
AddressSpaceBits = asBits;
|
||||
_addressSpaceSize = asSize;
|
||||
_pageTable = new PageTable<nuint>();
|
||||
_backingMemory = backingMemory;
|
||||
_pageTable = new PageTable<ulong>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps a virtual memory range into a physical memory range.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Addresses and size must be page aligned.
|
||||
/// </remarks>
|
||||
/// <param name="va">Virtual memory address</param>
|
||||
/// <param name="hostAddress">Physical memory address</param>
|
||||
/// <param name="size">Size to be mapped</param>
|
||||
public void Map(ulong va, nuint hostAddress, ulong size)
|
||||
/// <inheritdoc/>
|
||||
public void Map(ulong va, ulong pa, ulong size)
|
||||
{
|
||||
AssertValidAddressAndSize(va, size);
|
||||
|
||||
while (size != 0)
|
||||
{
|
||||
_pageTable.Map(va, hostAddress);
|
||||
_pageTable.Map(va, pa);
|
||||
|
||||
va += PageSize;
|
||||
hostAddress += PageSize;
|
||||
pa += PageSize;
|
||||
size -= PageSize;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unmaps a previously mapped range of virtual memory.
|
||||
/// </summary>
|
||||
/// <param name="va">Virtual address of the range to be unmapped</param>
|
||||
/// <param name="size">Size of the range to be unmapped</param>
|
||||
/// <inheritdoc/>
|
||||
public void Unmap(ulong va, ulong size)
|
||||
{
|
||||
AssertValidAddressAndSize(va, size);
|
||||
@ -88,47 +78,25 @@ namespace Ryujinx.Memory
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads data from mapped memory.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the data being read</typeparam>
|
||||
/// <param name="va">Virtual address of the data in memory</param>
|
||||
/// <returns>The data</returns>
|
||||
/// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
|
||||
/// <inheritdoc/>
|
||||
public T Read<T>(ulong va) where T : unmanaged
|
||||
{
|
||||
return MemoryMarshal.Cast<byte, T>(GetSpan(va, Unsafe.SizeOf<T>()))[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads data from mapped memory.
|
||||
/// </summary>
|
||||
/// <param name="va">Virtual address of the data in memory</param>
|
||||
/// <param name="data">Span to store the data being read into</param>
|
||||
/// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
|
||||
/// <inheritdoc/>
|
||||
public void Read(ulong va, Span<byte> data)
|
||||
{
|
||||
ReadImpl(va, data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes data to mapped memory.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the data being written</typeparam>
|
||||
/// <param name="va">Virtual address to write the data into</param>
|
||||
/// <param name="value">Data to be written</param>
|
||||
/// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
|
||||
/// <inheritdoc/>
|
||||
public void Write<T>(ulong va, T value) where T : unmanaged
|
||||
{
|
||||
Write(va, MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes data to mapped memory.
|
||||
/// </summary>
|
||||
/// <param name="va">Virtual address to write the data into</param>
|
||||
/// <param name="data">Data to be written</param>
|
||||
/// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
|
||||
/// <inheritdoc/>
|
||||
public void Write(ulong va, ReadOnlySpan<byte> data)
|
||||
{
|
||||
if (data.Length == 0)
|
||||
@ -140,7 +108,7 @@ namespace Ryujinx.Memory
|
||||
|
||||
if (IsContiguousAndMapped(va, data.Length))
|
||||
{
|
||||
data.CopyTo(GetHostSpanContiguous(va, data.Length));
|
||||
data.CopyTo(_backingMemory.GetSpan(GetPhysicalAddressInternal(va), data.Length));
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -148,34 +116,27 @@ namespace Ryujinx.Memory
|
||||
|
||||
if ((va & PageMask) != 0)
|
||||
{
|
||||
ulong pa = GetPhysicalAddressInternal(va);
|
||||
|
||||
size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
|
||||
|
||||
data.Slice(0, size).CopyTo(GetHostSpanContiguous(va, size));
|
||||
data.Slice(0, size).CopyTo(_backingMemory.GetSpan(pa, size));
|
||||
|
||||
offset += size;
|
||||
}
|
||||
|
||||
for (; offset < data.Length; offset += size)
|
||||
{
|
||||
ulong pa = GetPhysicalAddressInternal(va + (ulong)offset);
|
||||
|
||||
size = Math.Min(data.Length - offset, PageSize);
|
||||
|
||||
data.Slice(offset, size).CopyTo(GetHostSpanContiguous(va + (ulong)offset, size));
|
||||
data.Slice(offset, size).CopyTo(_backingMemory.GetSpan(pa, size));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a read-only span of data from mapped memory.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This may perform a allocation if the data is not contiguous in memory.
|
||||
/// For this reason, the span is read-only, you can't modify the data.
|
||||
/// </remarks>
|
||||
/// <param name="va">Virtual address of the data</param>
|
||||
/// <param name="size">Size of the data</param>
|
||||
/// <param name="tracked">True if read tracking is triggered on the span</param>
|
||||
/// <returns>A read-only span of the data</returns>
|
||||
/// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
|
||||
/// <inheritdoc/>
|
||||
public ReadOnlySpan<byte> GetSpan(ulong va, int size, bool tracked = false)
|
||||
{
|
||||
if (size == 0)
|
||||
@ -185,7 +146,7 @@ namespace Ryujinx.Memory
|
||||
|
||||
if (IsContiguousAndMapped(va, size))
|
||||
{
|
||||
return GetHostSpanContiguous(va, size);
|
||||
return _backingMemory.GetSpan(GetPhysicalAddressInternal(va), size);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -197,19 +158,7 @@ namespace Ryujinx.Memory
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a region of memory that can be written to.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If the requested region is not contiguous in physical memory,
|
||||
/// this will perform an allocation, and flush the data (writing it
|
||||
/// back to the backing memory) on disposal.
|
||||
/// </remarks>
|
||||
/// <param name="va">Virtual address of the data</param>
|
||||
/// <param name="size">Size of the data</param>
|
||||
/// <param name="tracked">True if write tracking is triggered on the span</param>
|
||||
/// <returns>A writable region of memory containing the data</returns>
|
||||
/// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
|
||||
/// <inheritdoc/>
|
||||
public unsafe WritableRegion GetWritableRegion(ulong va, int size, bool tracked = false)
|
||||
{
|
||||
if (size == 0)
|
||||
@ -219,7 +168,7 @@ namespace Ryujinx.Memory
|
||||
|
||||
if (IsContiguousAndMapped(va, size))
|
||||
{
|
||||
return new WritableRegion(null, va, new NativeMemoryManager<byte>((byte*)GetHostAddress(va), size).Memory);
|
||||
return new WritableRegion(null, va, _backingMemory.GetMemory(GetPhysicalAddressInternal(va), size));
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -231,33 +180,18 @@ namespace Ryujinx.Memory
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference for the given type at the specified virtual memory address.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The data must be located at a contiguous memory region.
|
||||
/// </remarks>
|
||||
/// <typeparam name="T">Type of the data to get the reference</typeparam>
|
||||
/// <param name="va">Virtual address of the data</param>
|
||||
/// <returns>A reference to the data in memory</returns>
|
||||
/// <exception cref="MemoryNotContiguousException">Throw if the specified memory region is not contiguous in physical memory</exception>
|
||||
public unsafe ref T GetRef<T>(ulong va) where T : unmanaged
|
||||
/// <inheritdoc/>
|
||||
public ref T GetRef<T>(ulong va) where T : unmanaged
|
||||
{
|
||||
if (!IsContiguous(va, Unsafe.SizeOf<T>()))
|
||||
{
|
||||
ThrowMemoryNotContiguous();
|
||||
}
|
||||
|
||||
return ref *(T*)GetHostAddress(va);
|
||||
return ref _backingMemory.GetRef<T>(GetPhysicalAddressInternal(va));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes the number of pages in a virtual address range.
|
||||
/// </summary>
|
||||
/// <param name="va">Virtual address of the range</param>
|
||||
/// <param name="size">Size of the range</param>
|
||||
/// <param name="startVa">The virtual address of the beginning of the first page</param>
|
||||
/// <remarks>This function does not differentiate between allocated and unallocated pages.</remarks>
|
||||
/// <inheritdoc/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private int GetPagesCount(ulong va, uint size, out ulong startVa)
|
||||
{
|
||||
@ -268,7 +202,7 @@ namespace Ryujinx.Memory
|
||||
return (int)(vaSpan / PageSize);
|
||||
}
|
||||
|
||||
private void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException();
|
||||
private static void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException();
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private bool IsContiguousAndMapped(ulong va, int size) => IsContiguous(va, size) && IsMapped(va);
|
||||
@ -290,7 +224,7 @@ namespace Ryujinx.Memory
|
||||
return false;
|
||||
}
|
||||
|
||||
if (GetHostAddress(va) + PageSize != GetHostAddress(va + PageSize))
|
||||
if (GetPhysicalAddressInternal(va) + PageSize != GetPhysicalAddressInternal(va + PageSize))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -301,18 +235,12 @@ namespace Ryujinx.Memory
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the physical regions that make up the given virtual address region.
|
||||
/// If any part of the virtual region is unmapped, null is returned.
|
||||
/// </summary>
|
||||
/// <param name="va">Virtual address of the range</param>
|
||||
/// <param name="size">Size of the range</param>
|
||||
/// <returns>Array of physical regions</returns>
|
||||
public IEnumerable<HostMemoryRange> GetPhysicalRegions(ulong va, ulong size)
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<MemoryRange> GetPhysicalRegions(ulong va, ulong size)
|
||||
{
|
||||
if (size == 0)
|
||||
{
|
||||
return Enumerable.Empty<HostMemoryRange>();
|
||||
return Enumerable.Empty<MemoryRange>();
|
||||
}
|
||||
|
||||
if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size))
|
||||
@ -322,9 +250,9 @@ namespace Ryujinx.Memory
|
||||
|
||||
int pages = GetPagesCount(va, (uint)size, out va);
|
||||
|
||||
var regions = new List<HostMemoryRange>();
|
||||
var regions = new List<MemoryRange>();
|
||||
|
||||
nuint regionStart = GetHostAddress(va);
|
||||
ulong regionStart = GetPhysicalAddressInternal(va);
|
||||
ulong regionSize = PageSize;
|
||||
|
||||
for (int page = 0; page < pages - 1; page++)
|
||||
@ -334,12 +262,12 @@ namespace Ryujinx.Memory
|
||||
return null;
|
||||
}
|
||||
|
||||
nuint newHostAddress = GetHostAddress(va + PageSize);
|
||||
ulong newPa = GetPhysicalAddressInternal(va + PageSize);
|
||||
|
||||
if (GetHostAddress(va) + PageSize != newHostAddress)
|
||||
if (GetPhysicalAddressInternal(va) + PageSize != newPa)
|
||||
{
|
||||
regions.Add(new HostMemoryRange(regionStart, regionSize));
|
||||
regionStart = newHostAddress;
|
||||
regions.Add(new MemoryRange(regionStart, regionSize));
|
||||
regionStart = newPa;
|
||||
regionSize = 0;
|
||||
}
|
||||
|
||||
@ -347,7 +275,7 @@ namespace Ryujinx.Memory
|
||||
regionSize += PageSize;
|
||||
}
|
||||
|
||||
regions.Add(new HostMemoryRange(regionStart, regionSize));
|
||||
regions.Add(new MemoryRange(regionStart, regionSize));
|
||||
|
||||
return regions;
|
||||
}
|
||||
@ -365,26 +293,26 @@ namespace Ryujinx.Memory
|
||||
|
||||
if ((va & PageMask) != 0)
|
||||
{
|
||||
ulong pa = GetPhysicalAddressInternal(va);
|
||||
|
||||
size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
|
||||
|
||||
GetHostSpanContiguous(va, size).CopyTo(data.Slice(0, size));
|
||||
_backingMemory.GetSpan(pa, size).CopyTo(data.Slice(0, size));
|
||||
|
||||
offset += size;
|
||||
}
|
||||
|
||||
for (; offset < data.Length; offset += size)
|
||||
{
|
||||
ulong pa = GetPhysicalAddressInternal(va + (ulong)offset);
|
||||
|
||||
size = Math.Min(data.Length - offset, PageSize);
|
||||
|
||||
GetHostSpanContiguous(va + (ulong)offset, size).CopyTo(data.Slice(offset, size));
|
||||
_backingMemory.GetSpan(pa, size).CopyTo(data.Slice(offset, size));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the page at a given virtual address is mapped.
|
||||
/// </summary>
|
||||
/// <param name="va">Virtual address to check</param>
|
||||
/// <returns>True if the address is mapped, false otherwise</returns>
|
||||
/// <inheritdoc/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool IsMapped(ulong va)
|
||||
{
|
||||
@ -396,12 +324,7 @@ namespace Ryujinx.Memory
|
||||
return _pageTable.Read(va) != 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a memory range is mapped.
|
||||
/// </summary>
|
||||
/// <param name="va">Virtual address of the range</param>
|
||||
/// <param name="size">Size of the range in bytes</param>
|
||||
/// <returns>True if the entire range is mapped, false otherwise</returns>
|
||||
/// <inheritdoc/>
|
||||
public bool IsRangeMapped(ulong va, ulong size)
|
||||
{
|
||||
if (size == 0UL)
|
||||
@ -460,14 +383,9 @@ namespace Ryujinx.Memory
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe Span<byte> GetHostSpanContiguous(ulong va, int size)
|
||||
private ulong GetPhysicalAddressInternal(ulong va)
|
||||
{
|
||||
return new Span<byte>((void*)GetHostAddress(va), size);
|
||||
}
|
||||
|
||||
private nuint GetHostAddress(ulong va)
|
||||
{
|
||||
return _pageTable.Read(va) + (nuint)(va & PageMask);
|
||||
return _pageTable.Read(va) + (va & PageMask);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -13,9 +13,9 @@ namespace Ryujinx.Memory
|
||||
/// Addresses and size must be page aligned.
|
||||
/// </remarks>
|
||||
/// <param name="va">Virtual memory address</param>
|
||||
/// <param name="hostAddress">Pointer where the region should be mapped to</param>
|
||||
/// <param name="pa">Physical memory address where the region should be mapped to</param>
|
||||
/// <param name="size">Size to be mapped</param>
|
||||
void Map(ulong va, nuint hostAddress, ulong size);
|
||||
void Map(ulong va, ulong pa, ulong size);
|
||||
|
||||
/// <summary>
|
||||
/// Unmaps a previously mapped range of virtual memory.
|
||||
@ -111,7 +111,7 @@ namespace Ryujinx.Memory
|
||||
/// <param name="va">Virtual address of the range</param>
|
||||
/// <param name="size">Size of the range</param>
|
||||
/// <returns>Array of physical regions</returns>
|
||||
IEnumerable<HostMemoryRange> GetPhysicalRegions(ulong va, ulong size);
|
||||
IEnumerable<MemoryRange> GetPhysicalRegions(ulong va, ulong size);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the page at a given CPU virtual address is mapped.
|
||||
|
@ -29,6 +29,18 @@ namespace Ryujinx.Memory
|
||||
/// Enables mirroring of the memory block through aliasing of memory pages.
|
||||
/// When enabled, this allows creating more memory blocks sharing the same backing storage.
|
||||
/// </summary>
|
||||
Mirrorable = 1 << 2
|
||||
Mirrorable = 1 << 2,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the memory block should support mapping views of a mirrorable memory block.
|
||||
/// The block that is to have their views mapped should be created with the <see cref="Mirrorable"/> flag.
|
||||
/// </summary>
|
||||
ViewCompatible = 1 << 3,
|
||||
|
||||
/// <summary>
|
||||
/// Forces views to be mapped page by page on Windows. When partial unmaps are done, this avoids the need
|
||||
/// to unmap the full range and remap sub-ranges, which creates a time window with incorrectly unmapped memory.
|
||||
/// </summary>
|
||||
ForceWindows4KBViewMapping = 1 << 4
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
|
||||
@ -11,8 +12,12 @@ namespace Ryujinx.Memory
|
||||
{
|
||||
private readonly bool _usesSharedMemory;
|
||||
private readonly bool _isMirror;
|
||||
private readonly bool _viewCompatible;
|
||||
private readonly bool _forceWindows4KBView;
|
||||
private IntPtr _sharedMemory;
|
||||
private IntPtr _pointer;
|
||||
private ConcurrentDictionary<MemoryBlock, byte> _viewStorages;
|
||||
private int _viewCount;
|
||||
|
||||
/// <summary>
|
||||
/// Pointer to the memory block data.
|
||||
@ -36,12 +41,14 @@ namespace Ryujinx.Memory
|
||||
if (flags.HasFlag(MemoryAllocationFlags.Mirrorable))
|
||||
{
|
||||
_sharedMemory = MemoryManagement.CreateSharedMemory(size, flags.HasFlag(MemoryAllocationFlags.Reserve));
|
||||
_pointer = MemoryManagement.MapSharedMemory(_sharedMemory);
|
||||
_pointer = MemoryManagement.MapSharedMemory(_sharedMemory, size);
|
||||
_usesSharedMemory = true;
|
||||
}
|
||||
else if (flags.HasFlag(MemoryAllocationFlags.Reserve))
|
||||
{
|
||||
_pointer = MemoryManagement.Reserve(size);
|
||||
_viewCompatible = flags.HasFlag(MemoryAllocationFlags.ViewCompatible);
|
||||
_forceWindows4KBView = flags.HasFlag(MemoryAllocationFlags.ForceWindows4KBViewMapping);
|
||||
_pointer = MemoryManagement.Reserve(size, _viewCompatible, _forceWindows4KBView);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -49,6 +56,10 @@ namespace Ryujinx.Memory
|
||||
}
|
||||
|
||||
Size = size;
|
||||
|
||||
_viewStorages = new ConcurrentDictionary<MemoryBlock, byte>();
|
||||
_viewStorages.TryAdd(this, 0);
|
||||
_viewCount = 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -60,7 +71,7 @@ namespace Ryujinx.Memory
|
||||
/// <exception cref="PlatformNotSupportedException">Throw when the current platform is not supported</exception>
|
||||
private MemoryBlock(ulong size, IntPtr sharedMemory)
|
||||
{
|
||||
_pointer = MemoryManagement.MapSharedMemory(sharedMemory);
|
||||
_pointer = MemoryManagement.MapSharedMemory(sharedMemory, size);
|
||||
Size = size;
|
||||
_usesSharedMemory = true;
|
||||
_isMirror = true;
|
||||
@ -112,6 +123,42 @@ namespace Ryujinx.Memory
|
||||
return MemoryManagement.Decommit(GetPointerInternal(offset, size), size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps a view of memory from another memory block.
|
||||
/// </summary>
|
||||
/// <param name="srcBlock">Memory block from where the backing memory will be taken</param>
|
||||
/// <param name="srcOffset">Offset on <paramref name="srcBlock"/> of the region that should be mapped</param>
|
||||
/// <param name="dstOffset">Offset to map the view into on this block</param>
|
||||
/// <param name="size">Size of the range to be mapped</param>
|
||||
/// <exception cref="NotSupportedException">Throw when the source memory block does not support mirroring</exception>
|
||||
/// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
|
||||
/// <exception cref="InvalidMemoryRegionException">Throw when either <paramref name="offset"/> or <paramref name="size"/> are out of range</exception>
|
||||
public void MapView(MemoryBlock srcBlock, ulong srcOffset, ulong dstOffset, ulong size)
|
||||
{
|
||||
if (srcBlock._sharedMemory == IntPtr.Zero)
|
||||
{
|
||||
throw new ArgumentException("The source memory block is not mirrorable, and thus cannot be mapped on the current block.");
|
||||
}
|
||||
|
||||
if (_viewStorages.TryAdd(srcBlock, 0))
|
||||
{
|
||||
srcBlock.IncrementViewCount();
|
||||
}
|
||||
|
||||
MemoryManagement.MapView(srcBlock._sharedMemory, srcOffset, GetPointerInternal(dstOffset, size), size, _forceWindows4KBView);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unmaps a view of memory from another memory block.
|
||||
/// </summary>
|
||||
/// <param name="srcBlock">Memory block from where the backing memory was taken during map</param>
|
||||
/// <param name="offset">Offset of the view previously mapped with <see cref="MapView"/></param>
|
||||
/// <param name="size">Size of the range to be unmapped</param>
|
||||
public void UnmapView(MemoryBlock srcBlock, ulong offset, ulong size)
|
||||
{
|
||||
MemoryManagement.UnmapView(srcBlock._sharedMemory, GetPointerInternal(offset, size), size, _forceWindows4KBView);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reprotects a region of memory.
|
||||
/// </summary>
|
||||
@ -124,21 +171,7 @@ namespace Ryujinx.Memory
|
||||
/// <exception cref="MemoryProtectionException">Throw when <paramref name="permission"/> is invalid</exception>
|
||||
public void Reprotect(ulong offset, ulong size, MemoryPermission permission, bool throwOnFail = true)
|
||||
{
|
||||
MemoryManagement.Reprotect(GetPointerInternal(offset, size), size, permission, throwOnFail);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remaps a region of memory into this memory block.
|
||||
/// </summary>
|
||||
/// <param name="offset">Starting offset of the range to be remapped into</param>
|
||||
/// <param name="sourceAddress">Starting offset of the range to be remapped from</param>
|
||||
/// <param name="size">Size of the range to be remapped</param>
|
||||
/// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
|
||||
/// <exception cref="InvalidMemoryRegionException">Throw when either <paramref name="offset"/> or <paramref name="size"/> are out of range</exception>
|
||||
/// <exception cref="MemoryProtectionException">Throw when <paramref name="permission"/> is invalid</exception>
|
||||
public void Remap(ulong offset, IntPtr sourceAddress, ulong size)
|
||||
{
|
||||
MemoryManagement.Remap(GetPointerInternal(offset, size), sourceAddress, size);
|
||||
MemoryManagement.Reprotect(GetPointerInternal(offset, size), size, permission, _viewCompatible, _forceWindows4KBView, throwOnFail);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -274,7 +307,7 @@ namespace Ryujinx.Memory
|
||||
/// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
|
||||
/// <exception cref="InvalidMemoryRegionException">Throw when either <paramref name="offset"/> or <paramref name="size"/> are out of range</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public nuint GetPointer(ulong offset, ulong size) => (nuint)(ulong)GetPointerInternal(offset, size);
|
||||
public IntPtr GetPointer(ulong offset, ulong size) => GetPointerInternal(offset, size);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private IntPtr GetPointerInternal(ulong offset, ulong size)
|
||||
@ -367,22 +400,63 @@ namespace Ryujinx.Memory
|
||||
{
|
||||
if (_usesSharedMemory)
|
||||
{
|
||||
MemoryManagement.UnmapSharedMemory(ptr);
|
||||
|
||||
if (_sharedMemory != IntPtr.Zero && !_isMirror)
|
||||
{
|
||||
MemoryManagement.DestroySharedMemory(_sharedMemory);
|
||||
_sharedMemory = IntPtr.Zero;
|
||||
}
|
||||
MemoryManagement.UnmapSharedMemory(ptr, Size);
|
||||
}
|
||||
else
|
||||
{
|
||||
MemoryManagement.Free(ptr);
|
||||
MemoryManagement.Free(ptr, Size, _forceWindows4KBView);
|
||||
}
|
||||
|
||||
foreach (MemoryBlock viewStorage in _viewStorages.Keys)
|
||||
{
|
||||
viewStorage.DecrementViewCount();
|
||||
}
|
||||
|
||||
_viewStorages.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
private void ThrowObjectDisposed() => throw new ObjectDisposedException(nameof(MemoryBlock));
|
||||
private void ThrowInvalidMemoryRegionException() => throw new InvalidMemoryRegionException();
|
||||
/// <summary>
|
||||
/// Increments the number of views that uses this memory block as storage.
|
||||
/// </summary>
|
||||
private void IncrementViewCount()
|
||||
{
|
||||
Interlocked.Increment(ref _viewCount);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrements the number of views that uses this memory block as storage.
|
||||
/// </summary>
|
||||
private void DecrementViewCount()
|
||||
{
|
||||
if (Interlocked.Decrement(ref _viewCount) == 0 && _sharedMemory != IntPtr.Zero && !_isMirror)
|
||||
{
|
||||
MemoryManagement.DestroySharedMemory(_sharedMemory);
|
||||
_sharedMemory = IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the specified memory allocation flags are supported on the current platform.
|
||||
/// </summary>
|
||||
/// <param name="flags">Flags to be checked</param>
|
||||
/// <returns>True if the platform supports all the flags, false otherwise</returns>
|
||||
public static bool SupportsFlags(MemoryAllocationFlags flags)
|
||||
{
|
||||
if (flags.HasFlag(MemoryAllocationFlags.ViewCompatible))
|
||||
{
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
return OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17134);
|
||||
}
|
||||
|
||||
return OperatingSystem.IsLinux() || OperatingSystem.IsMacOS();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void ThrowObjectDisposed() => throw new ObjectDisposedException(nameof(MemoryBlock));
|
||||
private static void ThrowInvalidMemoryRegionException() => throw new InvalidMemoryRegionException();
|
||||
}
|
||||
}
|
||||
|
@ -8,12 +8,9 @@ namespace Ryujinx.Memory
|
||||
{
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
IntPtr sizeNint = new IntPtr((long)size);
|
||||
|
||||
return MemoryManagementWindows.Allocate(sizeNint);
|
||||
return MemoryManagementWindows.Allocate((IntPtr)size);
|
||||
}
|
||||
else if (OperatingSystem.IsLinux() ||
|
||||
OperatingSystem.IsMacOS())
|
||||
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
||||
{
|
||||
return MemoryManagementUnix.Allocate(size);
|
||||
}
|
||||
@ -23,16 +20,13 @@ namespace Ryujinx.Memory
|
||||
}
|
||||
}
|
||||
|
||||
public static IntPtr Reserve(ulong size)
|
||||
public static IntPtr Reserve(ulong size, bool viewCompatible, bool force4KBMap)
|
||||
{
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
IntPtr sizeNint = new IntPtr((long)size);
|
||||
|
||||
return MemoryManagementWindows.Reserve(sizeNint);
|
||||
return MemoryManagementWindows.Reserve((IntPtr)size, viewCompatible, force4KBMap);
|
||||
}
|
||||
else if (OperatingSystem.IsLinux() ||
|
||||
OperatingSystem.IsMacOS())
|
||||
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
||||
{
|
||||
return MemoryManagementUnix.Reserve(size);
|
||||
}
|
||||
@ -46,12 +40,9 @@ namespace Ryujinx.Memory
|
||||
{
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
IntPtr sizeNint = new IntPtr((long)size);
|
||||
|
||||
return MemoryManagementWindows.Commit(address, sizeNint);
|
||||
return MemoryManagementWindows.Commit(address, (IntPtr)size);
|
||||
}
|
||||
else if (OperatingSystem.IsLinux() ||
|
||||
OperatingSystem.IsMacOS())
|
||||
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
||||
{
|
||||
return MemoryManagementUnix.Commit(address, size);
|
||||
}
|
||||
@ -65,12 +56,9 @@ namespace Ryujinx.Memory
|
||||
{
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
IntPtr sizeNint = new IntPtr((long)size);
|
||||
|
||||
return MemoryManagementWindows.Decommit(address, sizeNint);
|
||||
return MemoryManagementWindows.Decommit(address, (IntPtr)size);
|
||||
}
|
||||
else if (OperatingSystem.IsLinux() ||
|
||||
OperatingSystem.IsMacOS())
|
||||
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
||||
{
|
||||
return MemoryManagementUnix.Decommit(address, size);
|
||||
}
|
||||
@ -80,18 +68,68 @@ namespace Ryujinx.Memory
|
||||
}
|
||||
}
|
||||
|
||||
public static void Reprotect(IntPtr address, ulong size, MemoryPermission permission, bool throwOnFail)
|
||||
public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr address, ulong size, bool force4KBMap)
|
||||
{
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
if (force4KBMap)
|
||||
{
|
||||
MemoryManagementWindows.MapView4KB(sharedMemory, srcOffset, address, (IntPtr)size);
|
||||
}
|
||||
else
|
||||
{
|
||||
MemoryManagementWindows.MapView(sharedMemory, srcOffset, address, (IntPtr)size);
|
||||
}
|
||||
}
|
||||
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
||||
{
|
||||
MemoryManagementUnix.MapView(sharedMemory, srcOffset, address, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new PlatformNotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
public static void UnmapView(IntPtr sharedMemory, IntPtr address, ulong size, bool force4KBMap)
|
||||
{
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
if (force4KBMap)
|
||||
{
|
||||
MemoryManagementWindows.UnmapView4KB(address, (IntPtr)size);
|
||||
}
|
||||
else
|
||||
{
|
||||
MemoryManagementWindows.UnmapView(sharedMemory, address, (IntPtr)size);
|
||||
}
|
||||
}
|
||||
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
||||
{
|
||||
MemoryManagementUnix.UnmapView(address, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new PlatformNotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Reprotect(IntPtr address, ulong size, MemoryPermission permission, bool forView, bool force4KBMap, bool throwOnFail)
|
||||
{
|
||||
bool result;
|
||||
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
IntPtr sizeNint = new IntPtr((long)size);
|
||||
|
||||
result = MemoryManagementWindows.Reprotect(address, sizeNint, permission);
|
||||
if (forView && force4KBMap)
|
||||
{
|
||||
result = MemoryManagementWindows.Reprotect4KB(address, (IntPtr)size, permission, forView);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = MemoryManagementWindows.Reprotect(address, (IntPtr)size, permission, forView);
|
||||
}
|
||||
}
|
||||
else if (OperatingSystem.IsLinux() ||
|
||||
OperatingSystem.IsMacOS())
|
||||
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
||||
{
|
||||
result = MemoryManagementUnix.Reprotect(address, size, permission);
|
||||
}
|
||||
@ -106,14 +144,13 @@ namespace Ryujinx.Memory
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Free(IntPtr address)
|
||||
public static bool Free(IntPtr address, ulong size, bool force4KBMap)
|
||||
{
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
return MemoryManagementWindows.Free(address);
|
||||
return MemoryManagementWindows.Free(address, (IntPtr)size, force4KBMap);
|
||||
}
|
||||
else if (OperatingSystem.IsLinux() ||
|
||||
OperatingSystem.IsMacOS())
|
||||
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
||||
{
|
||||
return MemoryManagementUnix.Free(address);
|
||||
}
|
||||
@ -127,12 +164,9 @@ namespace Ryujinx.Memory
|
||||
{
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
IntPtr sizeNint = new IntPtr((long)size);
|
||||
|
||||
return MemoryManagementWindows.CreateSharedMemory(sizeNint, reserve);
|
||||
return MemoryManagementWindows.CreateSharedMemory((IntPtr)size, reserve);
|
||||
}
|
||||
else if (OperatingSystem.IsLinux() ||
|
||||
OperatingSystem.IsMacOS())
|
||||
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
||||
{
|
||||
return MemoryManagementUnix.CreateSharedMemory(size, reserve);
|
||||
}
|
||||
@ -148,8 +182,7 @@ namespace Ryujinx.Memory
|
||||
{
|
||||
MemoryManagementWindows.DestroySharedMemory(handle);
|
||||
}
|
||||
else if (OperatingSystem.IsLinux() ||
|
||||
OperatingSystem.IsMacOS())
|
||||
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
||||
{
|
||||
MemoryManagementUnix.DestroySharedMemory(handle);
|
||||
}
|
||||
@ -159,16 +192,15 @@ namespace Ryujinx.Memory
|
||||
}
|
||||
}
|
||||
|
||||
public static IntPtr MapSharedMemory(IntPtr handle)
|
||||
public static IntPtr MapSharedMemory(IntPtr handle, ulong size)
|
||||
{
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
return MemoryManagementWindows.MapSharedMemory(handle);
|
||||
}
|
||||
else if (OperatingSystem.IsLinux() ||
|
||||
OperatingSystem.IsMacOS())
|
||||
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
||||
{
|
||||
return MemoryManagementUnix.MapSharedMemory(handle);
|
||||
return MemoryManagementUnix.MapSharedMemory(handle, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -176,29 +208,15 @@ namespace Ryujinx.Memory
|
||||
}
|
||||
}
|
||||
|
||||
public static void UnmapSharedMemory(IntPtr address)
|
||||
public static void UnmapSharedMemory(IntPtr address, ulong size)
|
||||
{
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
MemoryManagementWindows.UnmapSharedMemory(address);
|
||||
}
|
||||
else if (OperatingSystem.IsLinux() ||
|
||||
OperatingSystem.IsMacOS())
|
||||
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
||||
{
|
||||
MemoryManagementUnix.UnmapSharedMemory(address);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new PlatformNotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
public static IntPtr Remap(IntPtr target, IntPtr source, ulong size)
|
||||
{
|
||||
if (OperatingSystem.IsLinux() ||
|
||||
OperatingSystem.IsMacOS())
|
||||
{
|
||||
return MemoryManagementUnix.Remap(target, source, size);
|
||||
MemoryManagementUnix.UnmapSharedMemory(address, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -1,8 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Versioning;
|
||||
using System.Text;
|
||||
|
||||
using static Ryujinx.Memory.MemoryManagerUnixHelper;
|
||||
|
||||
@ -12,15 +11,6 @@ namespace Ryujinx.Memory
|
||||
[SupportedOSPlatform("macos")]
|
||||
static class MemoryManagementUnix
|
||||
{
|
||||
private struct UnixSharedMemory
|
||||
{
|
||||
public IntPtr Pointer;
|
||||
public ulong Size;
|
||||
public IntPtr SourcePointer;
|
||||
}
|
||||
|
||||
private static readonly List<UnixSharedMemory> _sharedMemory = new List<UnixSharedMemory>();
|
||||
private static readonly ConcurrentDictionary<IntPtr, ulong> _sharedMemorySource = new ConcurrentDictionary<IntPtr, ulong>();
|
||||
private static readonly ConcurrentDictionary<IntPtr, ulong> _allocations = new ConcurrentDictionary<IntPtr, ulong>();
|
||||
|
||||
public static IntPtr Allocate(ulong size)
|
||||
@ -69,40 +59,15 @@ namespace Ryujinx.Memory
|
||||
|
||||
public static bool Commit(IntPtr address, ulong size)
|
||||
{
|
||||
bool success = mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) == 0;
|
||||
|
||||
if (success)
|
||||
{
|
||||
foreach (var shared in _sharedMemory)
|
||||
{
|
||||
if ((ulong)address + size > (ulong)shared.SourcePointer && (ulong)address < (ulong)shared.SourcePointer + shared.Size)
|
||||
{
|
||||
ulong sharedAddress = ((ulong)address - (ulong)shared.SourcePointer) + (ulong)shared.Pointer;
|
||||
|
||||
if (mprotect((IntPtr)sharedAddress, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return success;
|
||||
return mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) == 0;
|
||||
}
|
||||
|
||||
public static bool Decommit(IntPtr address, ulong size)
|
||||
{
|
||||
bool isShared;
|
||||
|
||||
lock (_sharedMemory)
|
||||
{
|
||||
isShared = _sharedMemory.Exists(x => (ulong)address >= (ulong)x.Pointer && (ulong)address + size <= (ulong)x.Pointer + x.Size);
|
||||
}
|
||||
|
||||
// Must be writable for madvise to work properly.
|
||||
mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE);
|
||||
|
||||
madvise(address, size, isShared ? MADV_REMOVE : MADV_DONTNEED);
|
||||
madvise(address, size, MADV_REMOVE);
|
||||
|
||||
return mprotect(address, size, MmapProts.PROT_NONE) == 0;
|
||||
}
|
||||
@ -136,139 +101,83 @@ namespace Ryujinx.Memory
|
||||
return false;
|
||||
}
|
||||
|
||||
public static IntPtr Remap(IntPtr target, IntPtr source, ulong size)
|
||||
public static bool Unmap(IntPtr address, ulong size)
|
||||
{
|
||||
int flags = 1;
|
||||
|
||||
if (target != IntPtr.Zero)
|
||||
{
|
||||
flags |= 2;
|
||||
}
|
||||
|
||||
IntPtr result = mremap(source, 0, size, flags, target);
|
||||
|
||||
if (result == IntPtr.Zero)
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
return result;
|
||||
return munmap(address, size) == 0;
|
||||
}
|
||||
|
||||
public static IntPtr CreateSharedMemory(ulong size, bool reserve)
|
||||
public unsafe static IntPtr CreateSharedMemory(ulong size, bool reserve)
|
||||
{
|
||||
IntPtr result = AllocateInternal(
|
||||
size,
|
||||
reserve ? MmapProts.PROT_NONE : MmapProts.PROT_READ | MmapProts.PROT_WRITE,
|
||||
true);
|
||||
int fd;
|
||||
|
||||
if (result == IntPtr.Zero)
|
||||
if (OperatingSystem.IsMacOS())
|
||||
{
|
||||
byte[] memName = Encoding.ASCII.GetBytes("Ryujinx-XXXXXX");
|
||||
|
||||
fixed (byte* pMemName = memName)
|
||||
{
|
||||
fd = shm_open((IntPtr)pMemName, 0x2 | 0x200 | 0x800 | 0x400, 384); // O_RDWR | O_CREAT | O_EXCL | O_TRUNC, 0600
|
||||
if (fd == -1)
|
||||
{
|
||||
throw new OutOfMemoryException();
|
||||
}
|
||||
|
||||
if (shm_unlink((IntPtr)pMemName) != 0)
|
||||
{
|
||||
throw new OutOfMemoryException();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] fileName = Encoding.ASCII.GetBytes("/dev/shm/Ryujinx-XXXXXX");
|
||||
|
||||
fixed (byte* pFileName = fileName)
|
||||
{
|
||||
fd = mkstemp((IntPtr)pFileName);
|
||||
if (fd == -1)
|
||||
{
|
||||
throw new OutOfMemoryException();
|
||||
}
|
||||
|
||||
if (unlink((IntPtr)pFileName) != 0)
|
||||
{
|
||||
throw new OutOfMemoryException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ftruncate(fd, (IntPtr)size) != 0)
|
||||
{
|
||||
throw new OutOfMemoryException();
|
||||
}
|
||||
|
||||
_sharedMemorySource[result] = (ulong)size;
|
||||
|
||||
return result;
|
||||
return (IntPtr)fd;
|
||||
}
|
||||
|
||||
public static void DestroySharedMemory(IntPtr handle)
|
||||
{
|
||||
lock (_sharedMemory)
|
||||
{
|
||||
foreach (var memory in _sharedMemory)
|
||||
{
|
||||
if (memory.SourcePointer == handle)
|
||||
{
|
||||
throw new InvalidOperationException("Shared memory cannot be destroyed unless fully unmapped.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_sharedMemorySource.Remove(handle, out ulong _);
|
||||
close((int)handle);
|
||||
}
|
||||
|
||||
public static IntPtr MapSharedMemory(IntPtr handle)
|
||||
public static IntPtr MapSharedMemory(IntPtr handle, ulong size)
|
||||
{
|
||||
// Try find the handle for this shared memory. If it is mapped, then we want to map
|
||||
// it a second time in another location.
|
||||
// If it is not mapped, then its handle is the mapping.
|
||||
|
||||
ulong size = _sharedMemorySource[handle];
|
||||
|
||||
if (size == 0)
|
||||
{
|
||||
throw new InvalidOperationException("Shared memory cannot be mapped after its source is unmapped.");
|
||||
}
|
||||
|
||||
lock (_sharedMemory)
|
||||
{
|
||||
foreach (var memory in _sharedMemory)
|
||||
{
|
||||
if (memory.Pointer == handle)
|
||||
{
|
||||
IntPtr result = AllocateInternal(
|
||||
memory.Size,
|
||||
MmapProts.PROT_NONE
|
||||
);
|
||||
|
||||
if (result == IntPtr.Zero)
|
||||
{
|
||||
throw new OutOfMemoryException();
|
||||
}
|
||||
|
||||
Remap(result, handle, memory.Size);
|
||||
|
||||
_sharedMemory.Add(new UnixSharedMemory
|
||||
{
|
||||
Pointer = result,
|
||||
Size = memory.Size,
|
||||
|
||||
SourcePointer = handle
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
_sharedMemory.Add(new UnixSharedMemory
|
||||
{
|
||||
Pointer = handle,
|
||||
Size = size,
|
||||
|
||||
SourcePointer = handle
|
||||
});
|
||||
}
|
||||
|
||||
return handle;
|
||||
return mmap(IntPtr.Zero, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE, MmapFlags.MAP_SHARED, (int)handle, 0);
|
||||
}
|
||||
|
||||
public static void UnmapSharedMemory(IntPtr address)
|
||||
public static void UnmapSharedMemory(IntPtr address, ulong size)
|
||||
{
|
||||
lock (_sharedMemory)
|
||||
{
|
||||
int removed = _sharedMemory.RemoveAll(memory =>
|
||||
{
|
||||
if (memory.Pointer == address)
|
||||
{
|
||||
if (memory.Pointer == memory.SourcePointer)
|
||||
{
|
||||
// After removing the original mapping, it cannot be mapped again.
|
||||
_sharedMemorySource[memory.SourcePointer] = 0;
|
||||
}
|
||||
munmap(address, size);
|
||||
}
|
||||
|
||||
Free(address);
|
||||
return true;
|
||||
}
|
||||
public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr location, ulong size)
|
||||
{
|
||||
mmap(location, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE, MmapFlags.MAP_FIXED | MmapFlags.MAP_SHARED, (int)sharedMemory, (long)srcOffset);
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
if (removed == 0)
|
||||
{
|
||||
throw new InvalidOperationException("Shared memory mapping could not be found.");
|
||||
}
|
||||
}
|
||||
public static void UnmapView(IntPtr location, ulong size)
|
||||
{
|
||||
mmap(location, size, MmapProts.PROT_NONE, MmapFlags.MAP_FIXED, -1, 0);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,5 @@
|
||||
using Ryujinx.Memory.WindowsShared;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Versioning;
|
||||
|
||||
namespace Ryujinx.Memory
|
||||
@ -9,74 +7,48 @@ namespace Ryujinx.Memory
|
||||
[SupportedOSPlatform("windows")]
|
||||
static class MemoryManagementWindows
|
||||
{
|
||||
private static readonly IntPtr InvalidHandleValue = new IntPtr(-1);
|
||||
private static bool UseWin10Placeholders;
|
||||
public const int PageSize = 0x1000;
|
||||
|
||||
private static object _emulatedHandleLock = new object();
|
||||
private static EmulatedSharedMemoryWindows[] _emulatedShared = new EmulatedSharedMemoryWindows[64];
|
||||
private static List<EmulatedSharedMemoryWindows> _emulatedSharedList = new List<EmulatedSharedMemoryWindows>();
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern IntPtr VirtualAlloc(
|
||||
IntPtr lpAddress,
|
||||
IntPtr dwSize,
|
||||
AllocationType flAllocationType,
|
||||
MemoryProtection flProtect);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern bool VirtualProtect(
|
||||
IntPtr lpAddress,
|
||||
IntPtr dwSize,
|
||||
MemoryProtection flNewProtect,
|
||||
out MemoryProtection lpflOldProtect);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern bool VirtualFree(IntPtr lpAddress, IntPtr dwSize, AllocationType dwFreeType);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern IntPtr CreateFileMapping(
|
||||
IntPtr hFile,
|
||||
IntPtr lpFileMappingAttributes,
|
||||
FileMapProtection flProtect,
|
||||
uint dwMaximumSizeHigh,
|
||||
uint dwMaximumSizeLow,
|
||||
[MarshalAs(UnmanagedType.LPWStr)] string lpName);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern bool CloseHandle(IntPtr hObject);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern IntPtr MapViewOfFile(
|
||||
IntPtr hFileMappingObject,
|
||||
uint dwDesiredAccess,
|
||||
uint dwFileOffsetHigh,
|
||||
uint dwFileOffsetLow,
|
||||
IntPtr dwNumberOfBytesToMap);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern uint GetLastError();
|
||||
|
||||
static MemoryManagementWindows()
|
||||
{
|
||||
UseWin10Placeholders = OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17134);
|
||||
}
|
||||
private static readonly PlaceholderManager _placeholders = new PlaceholderManager();
|
||||
private static readonly PlaceholderManager4KB _placeholders4KB = new PlaceholderManager4KB();
|
||||
|
||||
public static IntPtr Allocate(IntPtr size)
|
||||
{
|
||||
return AllocateInternal(size, AllocationType.Reserve | AllocationType.Commit);
|
||||
}
|
||||
|
||||
public static IntPtr Reserve(IntPtr size)
|
||||
public static IntPtr Reserve(IntPtr size, bool viewCompatible, bool force4KBMap)
|
||||
{
|
||||
if (viewCompatible)
|
||||
{
|
||||
IntPtr baseAddress = AllocateInternal2(size, AllocationType.Reserve | AllocationType.ReservePlaceholder);
|
||||
|
||||
if (!force4KBMap)
|
||||
{
|
||||
_placeholders.ReserveRange((ulong)baseAddress, (ulong)size);
|
||||
}
|
||||
|
||||
return baseAddress;
|
||||
}
|
||||
|
||||
return AllocateInternal(size, AllocationType.Reserve);
|
||||
}
|
||||
|
||||
private static IntPtr AllocateInternal(IntPtr size, AllocationType flags = 0)
|
||||
{
|
||||
IntPtr ptr = VirtualAlloc(IntPtr.Zero, size, flags, MemoryProtection.ReadWrite);
|
||||
IntPtr ptr = WindowsApi.VirtualAlloc(IntPtr.Zero, size, flags, MemoryProtection.ReadWrite);
|
||||
|
||||
if (ptr == IntPtr.Zero)
|
||||
{
|
||||
throw new OutOfMemoryException();
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
private static IntPtr AllocateInternal2(IntPtr size, AllocationType flags = 0)
|
||||
{
|
||||
IntPtr ptr = WindowsApi.VirtualAlloc2(WindowsApi.CurrentProcessHandle, IntPtr.Zero, size, flags, MemoryProtection.NoAccess, IntPtr.Zero, 0);
|
||||
|
||||
if (ptr == IntPtr.Zero)
|
||||
{
|
||||
@ -88,164 +60,129 @@ namespace Ryujinx.Memory
|
||||
|
||||
public static bool Commit(IntPtr location, IntPtr size)
|
||||
{
|
||||
if (UseWin10Placeholders)
|
||||
{
|
||||
lock (_emulatedSharedList)
|
||||
{
|
||||
foreach (var shared in _emulatedSharedList)
|
||||
{
|
||||
if (shared.CommitMap(location, size))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return VirtualAlloc(location, size, AllocationType.Commit, MemoryProtection.ReadWrite) != IntPtr.Zero;
|
||||
return WindowsApi.VirtualAlloc(location, size, AllocationType.Commit, MemoryProtection.ReadWrite) != IntPtr.Zero;
|
||||
}
|
||||
|
||||
public static bool Decommit(IntPtr location, IntPtr size)
|
||||
{
|
||||
if (UseWin10Placeholders)
|
||||
{
|
||||
lock (_emulatedSharedList)
|
||||
{
|
||||
foreach (var shared in _emulatedSharedList)
|
||||
{
|
||||
if (shared.DecommitMap(location, size))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return VirtualFree(location, size, AllocationType.Decommit);
|
||||
return WindowsApi.VirtualFree(location, size, AllocationType.Decommit);
|
||||
}
|
||||
|
||||
public static bool Reprotect(IntPtr address, IntPtr size, MemoryPermission permission)
|
||||
public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size)
|
||||
{
|
||||
if (UseWin10Placeholders)
|
||||
_placeholders.MapView(sharedMemory, srcOffset, location, size);
|
||||
}
|
||||
|
||||
public static void MapView4KB(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size)
|
||||
{
|
||||
_placeholders4KB.UnmapAndMarkRangeAsMapped(location, size);
|
||||
|
||||
ulong uaddress = (ulong)location;
|
||||
ulong usize = (ulong)size;
|
||||
IntPtr endLocation = (IntPtr)(uaddress + usize);
|
||||
|
||||
while (location != endLocation)
|
||||
{
|
||||
ulong uaddress = (ulong)address;
|
||||
ulong usize = (ulong)size;
|
||||
while (usize > 0)
|
||||
WindowsApi.VirtualFree(location, (IntPtr)PageSize, AllocationType.Release | AllocationType.PreservePlaceholder);
|
||||
|
||||
var ptr = WindowsApi.MapViewOfFile3(
|
||||
sharedMemory,
|
||||
WindowsApi.CurrentProcessHandle,
|
||||
location,
|
||||
srcOffset,
|
||||
(IntPtr)PageSize,
|
||||
0x4000,
|
||||
MemoryProtection.ReadWrite,
|
||||
IntPtr.Zero,
|
||||
0);
|
||||
|
||||
if (ptr == IntPtr.Zero)
|
||||
{
|
||||
ulong nextGranular = (uaddress & ~EmulatedSharedMemoryWindows.MappingMask) + EmulatedSharedMemoryWindows.MappingGranularity;
|
||||
ulong mapSize = Math.Min(usize, nextGranular - uaddress);
|
||||
|
||||
if (!VirtualProtect((IntPtr)uaddress, (IntPtr)mapSize, GetProtection(permission), out _))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
uaddress = nextGranular;
|
||||
usize -= mapSize;
|
||||
throw new WindowsApiException("MapViewOfFile3");
|
||||
}
|
||||
|
||||
return true;
|
||||
location += PageSize;
|
||||
srcOffset += PageSize;
|
||||
}
|
||||
}
|
||||
|
||||
public static void UnmapView(IntPtr sharedMemory, IntPtr location, IntPtr size)
|
||||
{
|
||||
_placeholders.UnmapView(sharedMemory, location, size);
|
||||
}
|
||||
|
||||
public static void UnmapView4KB(IntPtr location, IntPtr size)
|
||||
{
|
||||
_placeholders4KB.UnmapView(location, size);
|
||||
}
|
||||
|
||||
public static bool Reprotect(IntPtr address, IntPtr size, MemoryPermission permission, bool forView)
|
||||
{
|
||||
if (forView)
|
||||
{
|
||||
return _placeholders.ReprotectView(address, size, permission);
|
||||
}
|
||||
else
|
||||
{
|
||||
return VirtualProtect(address, size, GetProtection(permission), out _);
|
||||
return WindowsApi.VirtualProtect(address, size, WindowsApi.GetProtection(permission), out _);
|
||||
}
|
||||
}
|
||||
|
||||
private static MemoryProtection GetProtection(MemoryPermission permission)
|
||||
public static bool Reprotect4KB(IntPtr address, IntPtr size, MemoryPermission permission, bool forView)
|
||||
{
|
||||
return permission switch
|
||||
ulong uaddress = (ulong)address;
|
||||
ulong usize = (ulong)size;
|
||||
while (usize > 0)
|
||||
{
|
||||
MemoryPermission.None => MemoryProtection.NoAccess,
|
||||
MemoryPermission.Read => MemoryProtection.ReadOnly,
|
||||
MemoryPermission.ReadAndWrite => MemoryProtection.ReadWrite,
|
||||
MemoryPermission.ReadAndExecute => MemoryProtection.ExecuteRead,
|
||||
MemoryPermission.ReadWriteExecute => MemoryProtection.ExecuteReadWrite,
|
||||
MemoryPermission.Execute => MemoryProtection.Execute,
|
||||
_ => throw new MemoryProtectionException(permission)
|
||||
};
|
||||
}
|
||||
|
||||
public static bool Free(IntPtr address)
|
||||
{
|
||||
return VirtualFree(address, IntPtr.Zero, AllocationType.Release);
|
||||
}
|
||||
|
||||
private static int GetEmulatedHandle()
|
||||
{
|
||||
// Assumes we have the handle lock.
|
||||
|
||||
for (int i = 0; i < _emulatedShared.Length; i++)
|
||||
{
|
||||
if (_emulatedShared[i] == null)
|
||||
if (!WindowsApi.VirtualProtect((IntPtr)uaddress, (IntPtr)PageSize, WindowsApi.GetProtection(permission), out _))
|
||||
{
|
||||
return i + 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
uaddress += PageSize;
|
||||
usize -= PageSize;
|
||||
}
|
||||
|
||||
throw new InvalidProgramException("Too many shared memory handles were created.");
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool EmulatedHandleValid(ref int handle)
|
||||
public static bool Free(IntPtr address, IntPtr size, bool force4KBMap)
|
||||
{
|
||||
handle--;
|
||||
return handle >= 0 && handle < _emulatedShared.Length && _emulatedShared[handle] != null;
|
||||
if (force4KBMap)
|
||||
{
|
||||
_placeholders4KB.UnmapRange(address, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
_placeholders.UnmapView(IntPtr.Zero, address, size);
|
||||
}
|
||||
|
||||
return WindowsApi.VirtualFree(address, IntPtr.Zero, AllocationType.Release);
|
||||
}
|
||||
|
||||
public static IntPtr CreateSharedMemory(IntPtr size, bool reserve)
|
||||
{
|
||||
if (UseWin10Placeholders && reserve)
|
||||
var prot = reserve ? FileMapProtection.SectionReserve : FileMapProtection.SectionCommit;
|
||||
|
||||
IntPtr handle = WindowsApi.CreateFileMapping(
|
||||
WindowsApi.InvalidHandleValue,
|
||||
IntPtr.Zero,
|
||||
FileMapProtection.PageReadWrite | prot,
|
||||
(uint)(size.ToInt64() >> 32),
|
||||
(uint)size.ToInt64(),
|
||||
null);
|
||||
|
||||
if (handle == IntPtr.Zero)
|
||||
{
|
||||
lock (_emulatedHandleLock)
|
||||
{
|
||||
int handle = GetEmulatedHandle();
|
||||
_emulatedShared[handle - 1] = new EmulatedSharedMemoryWindows((ulong)size);
|
||||
_emulatedSharedList.Add(_emulatedShared[handle - 1]);
|
||||
|
||||
return (IntPtr)handle;
|
||||
}
|
||||
throw new OutOfMemoryException();
|
||||
}
|
||||
else
|
||||
{
|
||||
var prot = reserve ? FileMapProtection.SectionReserve : FileMapProtection.SectionCommit;
|
||||
|
||||
IntPtr handle = CreateFileMapping(
|
||||
InvalidHandleValue,
|
||||
IntPtr.Zero,
|
||||
FileMapProtection.PageReadWrite | prot,
|
||||
(uint)(size.ToInt64() >> 32),
|
||||
(uint)size.ToInt64(),
|
||||
null);
|
||||
|
||||
if (handle == IntPtr.Zero)
|
||||
{
|
||||
throw new OutOfMemoryException();
|
||||
}
|
||||
|
||||
return handle;
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
||||
public static void DestroySharedMemory(IntPtr handle)
|
||||
{
|
||||
if (UseWin10Placeholders)
|
||||
{
|
||||
lock (_emulatedHandleLock)
|
||||
{
|
||||
int iHandle = (int)(ulong)handle;
|
||||
|
||||
if (EmulatedHandleValid(ref iHandle))
|
||||
{
|
||||
_emulatedSharedList.Remove(_emulatedShared[iHandle]);
|
||||
_emulatedShared[iHandle].Dispose();
|
||||
_emulatedShared[iHandle] = null;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!CloseHandle(handle))
|
||||
if (!WindowsApi.CloseHandle(handle))
|
||||
{
|
||||
throw new ArgumentException("Invalid handle.", nameof(handle));
|
||||
}
|
||||
@ -253,20 +190,7 @@ namespace Ryujinx.Memory
|
||||
|
||||
public static IntPtr MapSharedMemory(IntPtr handle)
|
||||
{
|
||||
if (UseWin10Placeholders)
|
||||
{
|
||||
lock (_emulatedHandleLock)
|
||||
{
|
||||
int iHandle = (int)(ulong)handle;
|
||||
|
||||
if (EmulatedHandleValid(ref iHandle))
|
||||
{
|
||||
return _emulatedShared[iHandle].Map();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
IntPtr ptr = MapViewOfFile(handle, 4 | 2, 0, 0, IntPtr.Zero);
|
||||
IntPtr ptr = WindowsApi.MapViewOfFile(handle, 4 | 2, 0, 0, IntPtr.Zero);
|
||||
|
||||
if (ptr == IntPtr.Zero)
|
||||
{
|
||||
@ -278,24 +202,15 @@ namespace Ryujinx.Memory
|
||||
|
||||
public static void UnmapSharedMemory(IntPtr address)
|
||||
{
|
||||
if (UseWin10Placeholders)
|
||||
{
|
||||
lock (_emulatedHandleLock)
|
||||
{
|
||||
foreach (EmulatedSharedMemoryWindows shared in _emulatedSharedList)
|
||||
{
|
||||
if (shared.Unmap((ulong)address))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!UnmapViewOfFile(address))
|
||||
if (!WindowsApi.UnmapViewOfFile(address))
|
||||
{
|
||||
throw new ArgumentException("Invalid address.", nameof(address));
|
||||
}
|
||||
}
|
||||
|
||||
public static bool RetryFromAccessViolation()
|
||||
{
|
||||
return _placeholders.RetryFromAccessViolation();
|
||||
}
|
||||
}
|
||||
}
|
@ -21,7 +21,23 @@ namespace Ryujinx.Memory
|
||||
MAP_PRIVATE = 2,
|
||||
MAP_ANONYMOUS = 4,
|
||||
MAP_NORESERVE = 8,
|
||||
MAP_UNLOCKED = 16
|
||||
MAP_FIXED = 16,
|
||||
MAP_UNLOCKED = 32
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum OpenFlags : uint
|
||||
{
|
||||
O_RDONLY = 0,
|
||||
O_WRONLY = 1,
|
||||
O_RDWR = 2,
|
||||
O_CREAT = 4,
|
||||
O_EXCL = 8,
|
||||
O_NOCTTY = 16,
|
||||
O_TRUNC = 32,
|
||||
O_APPEND = 64,
|
||||
O_NONBLOCK = 128,
|
||||
O_SYNC = 256,
|
||||
}
|
||||
|
||||
private const int MAP_ANONYMOUS_LINUX_GENERIC = 0x20;
|
||||
@ -50,6 +66,24 @@ namespace Ryujinx.Memory
|
||||
[DllImport("libc", SetLastError = true)]
|
||||
public static extern int madvise(IntPtr address, ulong size, int advice);
|
||||
|
||||
[DllImport("libc", SetLastError = true)]
|
||||
public static extern int mkstemp(IntPtr template);
|
||||
|
||||
[DllImport("libc", SetLastError = true)]
|
||||
public static extern int unlink(IntPtr pathname);
|
||||
|
||||
[DllImport("libc", SetLastError = true)]
|
||||
public static extern int ftruncate(int fildes, IntPtr length);
|
||||
|
||||
[DllImport("libc", SetLastError = true)]
|
||||
public static extern int close(int fd);
|
||||
|
||||
[DllImport("libc", SetLastError = true)]
|
||||
public static extern int shm_open(IntPtr name, int oflag, uint mode);
|
||||
|
||||
[DllImport("libc", SetLastError = true)]
|
||||
public static extern int shm_unlink(IntPtr name);
|
||||
|
||||
private static int MmapFlagsToSystemFlags(MmapFlags flags)
|
||||
{
|
||||
int result = 0;
|
||||
@ -64,6 +98,11 @@ namespace Ryujinx.Memory
|
||||
result |= (int)MmapFlags.MAP_PRIVATE;
|
||||
}
|
||||
|
||||
if (flags.HasFlag(MmapFlags.MAP_FIXED))
|
||||
{
|
||||
result |= (int)MmapFlags.MAP_FIXED;
|
||||
}
|
||||
|
||||
if (flags.HasFlag(MmapFlags.MAP_ANONYMOUS))
|
||||
{
|
||||
if (OperatingSystem.IsLinux())
|
||||
|
@ -1,6 +1,6 @@
|
||||
namespace Ryujinx.Memory
|
||||
{
|
||||
class PageTable<T> where T : unmanaged
|
||||
public class PageTable<T> where T : unmanaged
|
||||
{
|
||||
public const int PageBits = 12;
|
||||
public const int PageSize = 1 << PageBits;
|
||||
|
@ -1,71 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Ryujinx.Memory.Range
|
||||
{
|
||||
/// <summary>
|
||||
/// Range of memory composed of an address and size.
|
||||
/// </summary>
|
||||
public struct HostMemoryRange : IEquatable<HostMemoryRange>
|
||||
{
|
||||
/// <summary>
|
||||
/// An empty memory range, with a null address and zero size.
|
||||
/// </summary>
|
||||
public static HostMemoryRange Empty => new HostMemoryRange(0, 0);
|
||||
|
||||
/// <summary>
|
||||
/// Start address of the range.
|
||||
/// </summary>
|
||||
public nuint Address { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Size of the range in bytes.
|
||||
/// </summary>
|
||||
public ulong Size { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Address where the range ends (exclusive).
|
||||
/// </summary>
|
||||
public nuint EndAddress => Address + (nuint)Size;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new memory range with the specified address and size.
|
||||
/// </summary>
|
||||
/// <param name="address">Start address</param>
|
||||
/// <param name="size">Size in bytes</param>
|
||||
public HostMemoryRange(nuint address, ulong size)
|
||||
{
|
||||
Address = address;
|
||||
Size = size;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the range overlaps with another.
|
||||
/// </summary>
|
||||
/// <param name="other">The other range to check for overlap</param>
|
||||
/// <returns>True if the ranges overlap, false otherwise</returns>
|
||||
public bool OverlapsWith(HostMemoryRange other)
|
||||
{
|
||||
nuint thisAddress = Address;
|
||||
nuint thisEndAddress = EndAddress;
|
||||
nuint otherAddress = other.Address;
|
||||
nuint otherEndAddress = other.EndAddress;
|
||||
|
||||
return thisAddress < otherEndAddress && otherAddress < thisEndAddress;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return obj is HostMemoryRange other && Equals(other);
|
||||
}
|
||||
|
||||
public bool Equals(HostMemoryRange other)
|
||||
{
|
||||
return Address == other.Address && Size == other.Size;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(Address, Size);
|
||||
}
|
||||
}
|
||||
}
|
@ -188,6 +188,30 @@ namespace Ryujinx.Memory.Tracking
|
||||
return VirtualMemoryEvent(address, 1, write);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Signal that a virtual memory event happened at the given location.
|
||||
/// This is similar VirtualMemoryEvent, but on Windows, it might also return true after a partial unmap.
|
||||
/// This should only be called from the exception handler.
|
||||
/// </summary>
|
||||
/// <param name="address">Virtual address accessed</param>
|
||||
/// <param name="size">Size of the region affected in bytes</param>
|
||||
/// <param name="write">Whether the region was written to or read</param>
|
||||
/// <param name="precise">True if the access is precise, false otherwise</param>
|
||||
/// <returns>True if the event triggered any tracking regions, false otherwise</returns>
|
||||
public bool VirtualMemoryEventEh(ulong address, ulong size, bool write, bool precise = false)
|
||||
{
|
||||
// Windows has a limitation, it can't do partial unmaps.
|
||||
// For this reason, we need to unmap the whole range and then remap the sub-ranges.
|
||||
// When this happens, we might have caused a undesirable access violation from the time that the range was unmapped.
|
||||
// In this case, try again as the memory might be mapped now.
|
||||
if (OperatingSystem.IsWindows() && MemoryManagementWindows.RetryFromAccessViolation())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return VirtualMemoryEvent(address, size, write, precise);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Signal that a virtual memory event happened at the given location.
|
||||
/// This can be flagged as a precise event, which will avoid reprotection and call special handlers if possible.
|
||||
|
@ -146,7 +146,9 @@ namespace Ryujinx.Memory.Tracking
|
||||
{
|
||||
_preAction?.Invoke(address, size);
|
||||
|
||||
_preAction = null;
|
||||
// The action is removed after it returns, to ensure that the null check above succeeds when
|
||||
// it's still in progress rather than continuing and possibly missing a required data flush.
|
||||
Interlocked.Exchange(ref _preAction, null);
|
||||
}
|
||||
}
|
||||
finally
|
||||
@ -252,8 +254,7 @@ namespace Ryujinx.Memory.Tracking
|
||||
|
||||
lock (_preActionLock)
|
||||
{
|
||||
RegionSignal lastAction = _preAction;
|
||||
_preAction = action;
|
||||
RegionSignal lastAction = Interlocked.Exchange(ref _preAction, action);
|
||||
|
||||
if (lastAction == null && action != lastAction)
|
||||
{
|
||||
|
@ -1,703 +0,0 @@
|
||||
using Ryujinx.Memory.Range;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Memory.WindowsShared
|
||||
{
|
||||
class EmulatedSharedMemoryWindows : IDisposable
|
||||
{
|
||||
private static readonly IntPtr InvalidHandleValue = new IntPtr(-1);
|
||||
private static readonly IntPtr CurrentProcessHandle = new IntPtr(-1);
|
||||
|
||||
public const int MappingBits = 16; // Windows 64kb granularity.
|
||||
public const ulong MappingGranularity = 1 << MappingBits;
|
||||
public const ulong MappingMask = MappingGranularity - 1;
|
||||
|
||||
public const ulong BackingSize32GB = 32UL * 1024UL * 1024UL * 1024UL; // Reasonable max size of 32GB.
|
||||
|
||||
private class SharedMemoryMapping : INonOverlappingRange
|
||||
{
|
||||
public ulong Address { get; }
|
||||
|
||||
public ulong Size { get; private set; }
|
||||
|
||||
public ulong EndAddress { get; private set; }
|
||||
|
||||
public List<int> Blocks;
|
||||
|
||||
public SharedMemoryMapping(ulong address, ulong size, List<int> blocks = null)
|
||||
{
|
||||
Address = address;
|
||||
Size = size;
|
||||
EndAddress = address + size;
|
||||
|
||||
Blocks = blocks ?? new List<int>();
|
||||
}
|
||||
|
||||
public bool OverlapsWith(ulong address, ulong size)
|
||||
{
|
||||
return Address < address + size && address < EndAddress;
|
||||
}
|
||||
|
||||
public void ExtendTo(ulong endAddress, RangeList<SharedMemoryMapping> list)
|
||||
{
|
||||
EndAddress = endAddress;
|
||||
Size = endAddress - Address;
|
||||
|
||||
list.UpdateEndAddress(this);
|
||||
}
|
||||
|
||||
public void AddBlocks(IEnumerable<int> blocks)
|
||||
{
|
||||
if (Blocks.Count > 0 && blocks.Count() > 0 && Blocks.Last() == blocks.First())
|
||||
{
|
||||
Blocks.AddRange(blocks.Skip(1));
|
||||
}
|
||||
else
|
||||
{
|
||||
Blocks.AddRange(blocks);
|
||||
}
|
||||
}
|
||||
|
||||
public INonOverlappingRange Split(ulong splitAddress)
|
||||
{
|
||||
SharedMemoryMapping newRegion = new SharedMemoryMapping(splitAddress, EndAddress - splitAddress);
|
||||
|
||||
int end = (int)((EndAddress + MappingMask) >> MappingBits);
|
||||
int start = (int)(Address >> MappingBits);
|
||||
|
||||
Size = splitAddress - Address;
|
||||
EndAddress = splitAddress;
|
||||
|
||||
int splitEndBlock = (int)((splitAddress + MappingMask) >> MappingBits);
|
||||
int splitStartBlock = (int)(splitAddress >> MappingBits);
|
||||
|
||||
newRegion.AddBlocks(Blocks.Skip(splitStartBlock - start));
|
||||
Blocks.RemoveRange(splitEndBlock - start, end - splitEndBlock);
|
||||
|
||||
return newRegion;
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern IntPtr CreateFileMapping(
|
||||
IntPtr hFile,
|
||||
IntPtr lpFileMappingAttributes,
|
||||
FileMapProtection flProtect,
|
||||
uint dwMaximumSizeHigh,
|
||||
uint dwMaximumSizeLow,
|
||||
[MarshalAs(UnmanagedType.LPWStr)] string lpName);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern bool CloseHandle(IntPtr hObject);
|
||||
|
||||
[DllImport("KernelBase.dll", SetLastError = true)]
|
||||
private static extern IntPtr VirtualAlloc2(
|
||||
IntPtr process,
|
||||
IntPtr lpAddress,
|
||||
IntPtr dwSize,
|
||||
AllocationType flAllocationType,
|
||||
MemoryProtection flProtect,
|
||||
IntPtr extendedParameters,
|
||||
ulong parameterCount);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern bool VirtualFree(IntPtr lpAddress, IntPtr dwSize, AllocationType dwFreeType);
|
||||
|
||||
[DllImport("KernelBase.dll", SetLastError = true)]
|
||||
private static extern IntPtr MapViewOfFile3(
|
||||
IntPtr hFileMappingObject,
|
||||
IntPtr process,
|
||||
IntPtr baseAddress,
|
||||
ulong offset,
|
||||
IntPtr dwNumberOfBytesToMap,
|
||||
ulong allocationType,
|
||||
MemoryProtection dwDesiredAccess,
|
||||
IntPtr extendedParameters,
|
||||
ulong parameterCount);
|
||||
|
||||
[DllImport("KernelBase.dll", SetLastError = true)]
|
||||
private static extern bool UnmapViewOfFile2(IntPtr process, IntPtr lpBaseAddress, ulong unmapFlags);
|
||||
|
||||
private ulong _size;
|
||||
|
||||
private object _lock = new object();
|
||||
|
||||
private ulong _backingSize;
|
||||
private IntPtr _backingMemHandle;
|
||||
private int _backingEnd;
|
||||
private int _backingAllocated;
|
||||
private Queue<int> _backingFreeList;
|
||||
|
||||
private List<ulong> _mappedBases;
|
||||
private RangeList<SharedMemoryMapping> _mappings;
|
||||
private SharedMemoryMapping[] _foundMappings = new SharedMemoryMapping[32];
|
||||
private PlaceholderList _placeholders;
|
||||
|
||||
public EmulatedSharedMemoryWindows(ulong size)
|
||||
{
|
||||
ulong backingSize = BackingSize32GB;
|
||||
|
||||
_size = size;
|
||||
_backingSize = backingSize;
|
||||
|
||||
_backingMemHandle = CreateFileMapping(
|
||||
InvalidHandleValue,
|
||||
IntPtr.Zero,
|
||||
FileMapProtection.PageReadWrite | FileMapProtection.SectionReserve,
|
||||
(uint)(backingSize >> 32),
|
||||
(uint)backingSize,
|
||||
null);
|
||||
|
||||
if (_backingMemHandle == IntPtr.Zero)
|
||||
{
|
||||
throw new OutOfMemoryException();
|
||||
}
|
||||
|
||||
_backingFreeList = new Queue<int>();
|
||||
_mappings = new RangeList<SharedMemoryMapping>();
|
||||
_mappedBases = new List<ulong>();
|
||||
_placeholders = new PlaceholderList(size >> MappingBits);
|
||||
}
|
||||
|
||||
private (ulong granularStart, ulong granularEnd) GetAlignedRange(ulong address, ulong size)
|
||||
{
|
||||
return (address & (~MappingMask), (address + size + MappingMask) & (~MappingMask));
|
||||
}
|
||||
|
||||
private void Commit(ulong address, ulong size)
|
||||
{
|
||||
(ulong granularStart, ulong granularEnd) = GetAlignedRange(address, size);
|
||||
|
||||
ulong endAddress = address + size;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
// Search a bit before and after the new mapping.
|
||||
// When adding our new mapping, we may need to join an existing mapping into our new mapping (or in some cases, to the other side!)
|
||||
ulong searchStart = granularStart == 0 ? 0 : (granularStart - 1);
|
||||
int mappingCount = _mappings.FindOverlapsNonOverlapping(searchStart, (granularEnd - searchStart) + 1, ref _foundMappings);
|
||||
|
||||
int first = -1;
|
||||
int last = -1;
|
||||
SharedMemoryMapping startOverlap = null;
|
||||
SharedMemoryMapping endOverlap = null;
|
||||
|
||||
int lastIndex = (int)(address >> MappingBits);
|
||||
int endIndex = (int)((endAddress + MappingMask) >> MappingBits);
|
||||
int firstBlock = -1;
|
||||
int endBlock = -1;
|
||||
|
||||
for (int i = 0; i < mappingCount; i++)
|
||||
{
|
||||
SharedMemoryMapping mapping = _foundMappings[i];
|
||||
|
||||
if (mapping.Address < address)
|
||||
{
|
||||
if (mapping.EndAddress >= address)
|
||||
{
|
||||
startOverlap = mapping;
|
||||
}
|
||||
|
||||
if ((int)((mapping.EndAddress - 1) >> MappingBits) == lastIndex)
|
||||
{
|
||||
lastIndex = (int)((mapping.EndAddress + MappingMask) >> MappingBits);
|
||||
firstBlock = mapping.Blocks.Last();
|
||||
}
|
||||
}
|
||||
|
||||
if (mapping.EndAddress > endAddress)
|
||||
{
|
||||
if (mapping.Address <= endAddress)
|
||||
{
|
||||
endOverlap = mapping;
|
||||
}
|
||||
|
||||
if ((int)((mapping.Address) >> MappingBits) + 1 == endIndex)
|
||||
{
|
||||
endIndex = (int)((mapping.Address) >> MappingBits);
|
||||
endBlock = mapping.Blocks.First();
|
||||
}
|
||||
}
|
||||
|
||||
if (mapping.OverlapsWith(address, size))
|
||||
{
|
||||
if (first == -1)
|
||||
{
|
||||
first = i;
|
||||
}
|
||||
|
||||
last = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (startOverlap == endOverlap && startOverlap != null)
|
||||
{
|
||||
// Already fully committed.
|
||||
return;
|
||||
}
|
||||
|
||||
var blocks = new List<int>();
|
||||
int lastBlock = -1;
|
||||
|
||||
if (firstBlock != -1)
|
||||
{
|
||||
blocks.Add(firstBlock);
|
||||
lastBlock = firstBlock;
|
||||
}
|
||||
|
||||
bool hasMapped = false;
|
||||
Action map = () =>
|
||||
{
|
||||
if (!hasMapped)
|
||||
{
|
||||
_placeholders.EnsurePlaceholders(address >> MappingBits, (granularEnd - granularStart) >> MappingBits, SplitPlaceholder);
|
||||
hasMapped = true;
|
||||
}
|
||||
|
||||
// There's a gap between this index and the last. Allocate blocks to fill it.
|
||||
blocks.Add(MapBackingBlock(MappingGranularity * (ulong)lastIndex++));
|
||||
};
|
||||
|
||||
if (first != -1)
|
||||
{
|
||||
for (int i = first; i <= last; i++)
|
||||
{
|
||||
SharedMemoryMapping mapping = _foundMappings[i];
|
||||
int mapIndex = (int)(mapping.Address >> MappingBits);
|
||||
|
||||
while (lastIndex < mapIndex)
|
||||
{
|
||||
map();
|
||||
}
|
||||
|
||||
if (lastBlock == mapping.Blocks[0])
|
||||
{
|
||||
blocks.AddRange(mapping.Blocks.Skip(1));
|
||||
}
|
||||
else
|
||||
{
|
||||
blocks.AddRange(mapping.Blocks);
|
||||
}
|
||||
|
||||
lastIndex = (int)((mapping.EndAddress - 1) >> MappingBits) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
while (lastIndex < endIndex)
|
||||
{
|
||||
map();
|
||||
}
|
||||
|
||||
if (endBlock != -1 && endBlock != lastBlock)
|
||||
{
|
||||
blocks.Add(endBlock);
|
||||
}
|
||||
|
||||
if (startOverlap != null && endOverlap != null)
|
||||
{
|
||||
// Both sides should be coalesced. Extend the start overlap to contain the end overlap, and add together their blocks.
|
||||
|
||||
_mappings.Remove(endOverlap);
|
||||
|
||||
startOverlap.ExtendTo(endOverlap.EndAddress, _mappings);
|
||||
|
||||
startOverlap.AddBlocks(blocks);
|
||||
startOverlap.AddBlocks(endOverlap.Blocks);
|
||||
}
|
||||
else if (startOverlap != null)
|
||||
{
|
||||
startOverlap.ExtendTo(endAddress, _mappings);
|
||||
|
||||
startOverlap.AddBlocks(blocks);
|
||||
}
|
||||
else
|
||||
{
|
||||
var mapping = new SharedMemoryMapping(address, size, blocks);
|
||||
|
||||
if (endOverlap != null)
|
||||
{
|
||||
mapping.ExtendTo(endOverlap.EndAddress, _mappings);
|
||||
|
||||
mapping.AddBlocks(endOverlap.Blocks);
|
||||
|
||||
_mappings.Remove(endOverlap);
|
||||
}
|
||||
|
||||
_mappings.Add(mapping);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Decommit(ulong address, ulong size)
|
||||
{
|
||||
(ulong granularStart, ulong granularEnd) = GetAlignedRange(address, size);
|
||||
ulong endAddress = address + size;
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
int mappingCount = _mappings.FindOverlapsNonOverlapping(granularStart, granularEnd - granularStart, ref _foundMappings);
|
||||
|
||||
int first = -1;
|
||||
int last = -1;
|
||||
|
||||
for (int i = 0; i < mappingCount; i++)
|
||||
{
|
||||
SharedMemoryMapping mapping = _foundMappings[i];
|
||||
|
||||
if (mapping.OverlapsWith(address, size))
|
||||
{
|
||||
if (first == -1)
|
||||
{
|
||||
first = i;
|
||||
}
|
||||
|
||||
last = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (first == -1)
|
||||
{
|
||||
return; // Could not find any regions to decommit.
|
||||
}
|
||||
|
||||
int lastReleasedBlock = -1;
|
||||
|
||||
bool releasedFirst = false;
|
||||
bool releasedLast = false;
|
||||
|
||||
for (int i = last; i >= first; i--)
|
||||
{
|
||||
SharedMemoryMapping mapping = _foundMappings[i];
|
||||
bool releaseEnd = true;
|
||||
bool releaseStart = true;
|
||||
|
||||
if (i == last)
|
||||
{
|
||||
// If this is the last region, do not release the block if there is a page ahead of us, or the block continues after us. (it is keeping the block alive)
|
||||
releaseEnd = last == mappingCount - 1;
|
||||
|
||||
// If the end region starts after the decommit end address, split and readd it after modifying its base address.
|
||||
if (mapping.EndAddress > endAddress)
|
||||
{
|
||||
var newMapping = (SharedMemoryMapping)mapping.Split(endAddress);
|
||||
_mappings.UpdateEndAddress(mapping);
|
||||
_mappings.Add(newMapping);
|
||||
|
||||
if ((endAddress & MappingMask) != 0)
|
||||
{
|
||||
releaseEnd = false;
|
||||
}
|
||||
}
|
||||
|
||||
releasedLast = releaseEnd;
|
||||
}
|
||||
|
||||
if (i == first)
|
||||
{
|
||||
// If this is the first region, do not release the block if there is a region behind us. (it is keeping the block alive)
|
||||
releaseStart = first == 0;
|
||||
|
||||
// If the first region starts before the decommit address, split it by modifying its end address.
|
||||
if (mapping.Address < address)
|
||||
{
|
||||
var oldMapping = mapping;
|
||||
mapping = (SharedMemoryMapping)mapping.Split(address);
|
||||
_mappings.UpdateEndAddress(oldMapping);
|
||||
|
||||
if ((address & MappingMask) != 0)
|
||||
{
|
||||
releaseStart = false;
|
||||
}
|
||||
}
|
||||
|
||||
releasedFirst = releaseStart;
|
||||
}
|
||||
|
||||
_mappings.Remove(mapping);
|
||||
|
||||
ulong releasePointer = (mapping.EndAddress + MappingMask) & (~MappingMask);
|
||||
for (int j = mapping.Blocks.Count - 1; j >= 0; j--)
|
||||
{
|
||||
int blockId = mapping.Blocks[j];
|
||||
|
||||
releasePointer -= MappingGranularity;
|
||||
|
||||
if (lastReleasedBlock == blockId)
|
||||
{
|
||||
// When committed regions are fragmented, multiple will have the same block id for their start/end granular block.
|
||||
// Avoid releasing these blocks twice.
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((j != 0 || releaseStart) && (j != mapping.Blocks.Count - 1 || releaseEnd))
|
||||
{
|
||||
ReleaseBackingBlock(releasePointer, blockId);
|
||||
}
|
||||
|
||||
lastReleasedBlock = blockId;
|
||||
}
|
||||
}
|
||||
|
||||
ulong placeholderStart = (granularStart >> MappingBits) + (releasedFirst ? 0UL : 1UL);
|
||||
ulong placeholderEnd = (granularEnd >> MappingBits) - (releasedLast ? 0UL : 1UL);
|
||||
|
||||
if (placeholderEnd > placeholderStart)
|
||||
{
|
||||
_placeholders.RemovePlaceholders(placeholderStart, placeholderEnd - placeholderStart, CoalescePlaceholder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool CommitMap(IntPtr address, IntPtr size)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
foreach (ulong mapping in _mappedBases)
|
||||
{
|
||||
ulong offset = (ulong)address - mapping;
|
||||
|
||||
if (offset < _size)
|
||||
{
|
||||
Commit(offset, (ulong)size);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool DecommitMap(IntPtr address, IntPtr size)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
foreach (ulong mapping in _mappedBases)
|
||||
{
|
||||
ulong offset = (ulong)address - mapping;
|
||||
|
||||
if (offset < _size)
|
||||
{
|
||||
Decommit(offset, (ulong)size);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private int MapBackingBlock(ulong offset)
|
||||
{
|
||||
bool allocate = false;
|
||||
int backing;
|
||||
|
||||
if (_backingFreeList.Count > 0)
|
||||
{
|
||||
backing = _backingFreeList.Dequeue();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_backingAllocated == _backingEnd)
|
||||
{
|
||||
// Allocate the backing.
|
||||
_backingAllocated++;
|
||||
allocate = true;
|
||||
}
|
||||
|
||||
backing = _backingEnd++;
|
||||
}
|
||||
|
||||
ulong backingOffset = MappingGranularity * (ulong)backing;
|
||||
|
||||
foreach (ulong baseAddress in _mappedBases)
|
||||
{
|
||||
CommitToMap(baseAddress, offset, MappingGranularity, backingOffset, allocate);
|
||||
allocate = false;
|
||||
}
|
||||
|
||||
return backing;
|
||||
}
|
||||
|
||||
private void ReleaseBackingBlock(ulong offset, int id)
|
||||
{
|
||||
foreach (ulong baseAddress in _mappedBases)
|
||||
{
|
||||
DecommitFromMap(baseAddress, offset);
|
||||
}
|
||||
|
||||
if (_backingEnd - 1 == id)
|
||||
{
|
||||
_backingEnd = id;
|
||||
}
|
||||
else
|
||||
{
|
||||
_backingFreeList.Enqueue(id);
|
||||
}
|
||||
}
|
||||
|
||||
public IntPtr Map()
|
||||
{
|
||||
IntPtr newMapping = VirtualAlloc2(
|
||||
CurrentProcessHandle,
|
||||
IntPtr.Zero,
|
||||
(IntPtr)_size,
|
||||
AllocationType.Reserve | AllocationType.ReservePlaceholder,
|
||||
MemoryProtection.NoAccess,
|
||||
IntPtr.Zero,
|
||||
0);
|
||||
|
||||
if (newMapping == IntPtr.Zero)
|
||||
{
|
||||
throw new OutOfMemoryException();
|
||||
}
|
||||
|
||||
// Apply all existing mappings to the new mapping
|
||||
lock (_lock)
|
||||
{
|
||||
int lastBlock = -1;
|
||||
foreach (SharedMemoryMapping mapping in _mappings)
|
||||
{
|
||||
ulong blockAddress = mapping.Address & (~MappingMask);
|
||||
foreach (int block in mapping.Blocks)
|
||||
{
|
||||
if (block != lastBlock)
|
||||
{
|
||||
ulong backingOffset = MappingGranularity * (ulong)block;
|
||||
|
||||
CommitToMap((ulong)newMapping, blockAddress, MappingGranularity, backingOffset, false);
|
||||
|
||||
lastBlock = block;
|
||||
}
|
||||
|
||||
blockAddress += MappingGranularity;
|
||||
}
|
||||
}
|
||||
|
||||
_mappedBases.Add((ulong)newMapping);
|
||||
}
|
||||
|
||||
return newMapping;
|
||||
}
|
||||
|
||||
private void SplitPlaceholder(ulong address, ulong size)
|
||||
{
|
||||
ulong byteAddress = address << MappingBits;
|
||||
IntPtr byteSize = (IntPtr)(size << MappingBits);
|
||||
|
||||
foreach (ulong mapAddress in _mappedBases)
|
||||
{
|
||||
bool result = VirtualFree((IntPtr)(mapAddress + byteAddress), byteSize, AllocationType.PreservePlaceholder | AllocationType.Release);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
throw new InvalidOperationException("Placeholder could not be split.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CoalescePlaceholder(ulong address, ulong size)
|
||||
{
|
||||
ulong byteAddress = address << MappingBits;
|
||||
IntPtr byteSize = (IntPtr)(size << MappingBits);
|
||||
|
||||
foreach (ulong mapAddress in _mappedBases)
|
||||
{
|
||||
bool result = VirtualFree((IntPtr)(mapAddress + byteAddress), byteSize, AllocationType.CoalescePlaceholders | AllocationType.Release);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
throw new InvalidOperationException("Placeholder could not be coalesced.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CommitToMap(ulong mapAddress, ulong address, ulong size, ulong backingOffset, bool allocate)
|
||||
{
|
||||
IntPtr targetAddress = (IntPtr)(mapAddress + address);
|
||||
|
||||
// Assume the placeholder worked (or already exists)
|
||||
// Map the backing memory into the mapped location.
|
||||
|
||||
IntPtr mapped = MapViewOfFile3(
|
||||
_backingMemHandle,
|
||||
CurrentProcessHandle,
|
||||
targetAddress,
|
||||
backingOffset,
|
||||
(IntPtr)MappingGranularity,
|
||||
0x4000, // REPLACE_PLACEHOLDER
|
||||
MemoryProtection.ReadWrite,
|
||||
IntPtr.Zero,
|
||||
0);
|
||||
|
||||
if (mapped == IntPtr.Zero)
|
||||
{
|
||||
throw new InvalidOperationException($"Could not map view of backing memory. (va=0x{address:X16} size=0x{size:X16}, error code {Marshal.GetLastWin32Error()})");
|
||||
}
|
||||
|
||||
if (allocate)
|
||||
{
|
||||
// Commit this part of the shared memory.
|
||||
VirtualAlloc2(CurrentProcessHandle, targetAddress, (IntPtr)MappingGranularity, AllocationType.Commit, MemoryProtection.ReadWrite, IntPtr.Zero, 0);
|
||||
}
|
||||
}
|
||||
|
||||
private void DecommitFromMap(ulong baseAddress, ulong address)
|
||||
{
|
||||
UnmapViewOfFile2(CurrentProcessHandle, (IntPtr)(baseAddress + address), 2);
|
||||
}
|
||||
|
||||
public bool Unmap(ulong baseAddress)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_mappedBases.Remove(baseAddress))
|
||||
{
|
||||
int lastBlock = -1;
|
||||
|
||||
foreach (SharedMemoryMapping mapping in _mappings)
|
||||
{
|
||||
ulong blockAddress = mapping.Address & (~MappingMask);
|
||||
foreach (int block in mapping.Blocks)
|
||||
{
|
||||
if (block != lastBlock)
|
||||
{
|
||||
DecommitFromMap(baseAddress, blockAddress);
|
||||
|
||||
lastBlock = block;
|
||||
}
|
||||
|
||||
blockAddress += MappingGranularity;
|
||||
}
|
||||
}
|
||||
|
||||
if (!VirtualFree((IntPtr)baseAddress, (IntPtr)0, AllocationType.Release))
|
||||
{
|
||||
throw new InvalidOperationException("Couldn't free mapping placeholder.");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// Remove all file mappings
|
||||
lock (_lock)
|
||||
{
|
||||
foreach (ulong baseAddress in _mappedBases.ToArray())
|
||||
{
|
||||
Unmap(baseAddress);
|
||||
}
|
||||
}
|
||||
|
||||
// Finally, delete the file mapping.
|
||||
CloseHandle(_backingMemHandle);
|
||||
}
|
||||
}
|
||||
}
|
740
Ryujinx.Memory/WindowsShared/IntervalTree.cs
Normal file
740
Ryujinx.Memory/WindowsShared/IntervalTree.cs
Normal file
@ -0,0 +1,740 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ryujinx.Memory.WindowsShared
|
||||
{
|
||||
/// <summary>
|
||||
/// An Augmented Interval Tree based off of the "TreeDictionary"'s Red-Black Tree. Allows fast overlap checking of ranges.
|
||||
/// </summary>
|
||||
/// <typeparam name="K">Key</typeparam>
|
||||
/// <typeparam name="V">Value</typeparam>
|
||||
class IntervalTree<K, V> where K : IComparable<K>
|
||||
{
|
||||
private const int ArrayGrowthSize = 32;
|
||||
|
||||
private const bool Black = true;
|
||||
private const bool Red = false;
|
||||
private IntervalTreeNode<K, V> _root = null;
|
||||
private int _count = 0;
|
||||
|
||||
public int Count => _count;
|
||||
|
||||
public IntervalTree() { }
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>
|
||||
/// Gets the values of the interval whose key is <paramref name="key"/>.
|
||||
/// </summary>
|
||||
/// <param name="key">Key of the node value to get</param>
|
||||
/// <param name="value">Value with the given <paramref name="key"/></param>
|
||||
/// <returns>True if the key is on the dictionary, false otherwise</returns>
|
||||
public bool TryGet(K key, out V value)
|
||||
{
|
||||
IntervalTreeNode<K, V> node = GetNode(key);
|
||||
|
||||
if (node == null)
|
||||
{
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
value = node.Value;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the start addresses of the intervals whose start and end keys overlap the given range.
|
||||
/// </summary>
|
||||
/// <param name="start">Start of the range</param>
|
||||
/// <param name="end">End of the range</param>
|
||||
/// <param name="overlaps">Overlaps array to place results in</param>
|
||||
/// <param name="overlapCount">Index to start writing results into the array. Defaults to 0</param>
|
||||
/// <returns>Number of intervals found</returns>
|
||||
public int Get(K start, K end, ref IntervalTreeNode<K, V>[] overlaps, int overlapCount = 0)
|
||||
{
|
||||
GetNodes(_root, start, end, ref overlaps, ref overlapCount);
|
||||
|
||||
return overlapCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new interval into the tree whose start is <paramref name="start"/>, end is <paramref name="end"/> and value is <paramref name="value"/>.
|
||||
/// </summary>
|
||||
/// <param name="start">Start of the range to add</param>
|
||||
/// <param name="end">End of the range to insert</param>
|
||||
/// <param name="value">Value to add</param>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null</exception>
|
||||
public void Add(K start, K end, V value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
BSTInsert(start, end, value, null, out _);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a value from the tree, searching for it with <paramref name="key"/>.
|
||||
/// </summary>
|
||||
/// <param name="key">Key of the node to remove</param>
|
||||
/// <returns>Number of deleted values</returns>
|
||||
public int Remove(K key)
|
||||
{
|
||||
return Remove(GetNode(key));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a value from the tree, searching for it with <paramref name="key"/>.
|
||||
/// </summary>
|
||||
/// <param name="nodeToDelete">Node to be removed</param>
|
||||
/// <returns>Number of deleted values</returns>
|
||||
public int Remove(IntervalTreeNode<K, V> nodeToDelete)
|
||||
{
|
||||
if (nodeToDelete == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
Delete(nodeToDelete);
|
||||
|
||||
_count--;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds all the nodes in the dictionary into <paramref name="list"/>.
|
||||
/// </summary>
|
||||
/// <returns>A list of all values sorted by Key Order</returns>
|
||||
public List<V> AsList()
|
||||
{
|
||||
List<V> list = new List<V>();
|
||||
|
||||
AddToList(_root, list);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods (BST)
|
||||
|
||||
/// <summary>
|
||||
/// Adds all values that are children of or contained within <paramref name="node"/> into <paramref name="list"/>, in Key Order.
|
||||
/// </summary>
|
||||
/// <param name="node">The node to search for values within</param>
|
||||
/// <param name="list">The list to add values to</param>
|
||||
private void AddToList(IntervalTreeNode<K, V> node, List<V> list)
|
||||
{
|
||||
if (node == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
AddToList(node.Left, list);
|
||||
|
||||
list.Add(node.Value);
|
||||
|
||||
AddToList(node.Right, list);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve the node reference whose key is <paramref name="key"/>, or null if no such node exists.
|
||||
/// </summary>
|
||||
/// <param name="key">Key of the node to get</param>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null</exception>
|
||||
/// <returns>Node reference in the tree</returns>
|
||||
private IntervalTreeNode<K, V> GetNode(K key)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
IntervalTreeNode<K, V> node = _root;
|
||||
while (node != null)
|
||||
{
|
||||
int cmp = key.CompareTo(node.Start);
|
||||
if (cmp < 0)
|
||||
{
|
||||
node = node.Left;
|
||||
}
|
||||
else if (cmp > 0)
|
||||
{
|
||||
node = node.Right;
|
||||
}
|
||||
else
|
||||
{
|
||||
return node;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve all nodes that overlap the given start and end keys.
|
||||
/// </summary>
|
||||
/// <param name="start">Start of the range</param>
|
||||
/// <param name="end">End of the range</param>
|
||||
/// <param name="overlaps">Overlaps array to place results in</param>
|
||||
/// <param name="overlapCount">Overlaps count to update</param>
|
||||
private void GetNodes(IntervalTreeNode<K, V> node, K start, K end, ref IntervalTreeNode<K, V>[] overlaps, ref int overlapCount)
|
||||
{
|
||||
if (node == null || start.CompareTo(node.Max) >= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GetNodes(node.Left, start, end, ref overlaps, ref overlapCount);
|
||||
|
||||
bool endsOnRight = end.CompareTo(node.Start) > 0;
|
||||
if (endsOnRight)
|
||||
{
|
||||
if (start.CompareTo(node.End) < 0)
|
||||
{
|
||||
if (overlaps.Length >= overlapCount)
|
||||
{
|
||||
Array.Resize(ref overlaps, overlapCount + ArrayGrowthSize);
|
||||
}
|
||||
|
||||
overlaps[overlapCount++] = node;
|
||||
}
|
||||
|
||||
GetNodes(node.Right, start, end, ref overlaps, ref overlapCount);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Propagate an increase in max value starting at the given node, heading up the tree.
|
||||
/// This should only be called if the max increases - not for rebalancing or removals.
|
||||
/// </summary>
|
||||
/// <param name="node">The node to start propagating from</param>
|
||||
private void PropagateIncrease(IntervalTreeNode<K, V> node)
|
||||
{
|
||||
K max = node.Max;
|
||||
IntervalTreeNode<K, V> ptr = node;
|
||||
|
||||
while ((ptr = ptr.Parent) != null)
|
||||
{
|
||||
if (max.CompareTo(ptr.Max) > 0)
|
||||
{
|
||||
ptr.Max = max;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Propagate recalculating max value starting at the given node, heading up the tree.
|
||||
/// This fully recalculates the max value from all children when there is potential for it to decrease.
|
||||
/// </summary>
|
||||
/// <param name="node">The node to start propagating from</param>
|
||||
private void PropagateFull(IntervalTreeNode<K, V> node)
|
||||
{
|
||||
IntervalTreeNode<K, V> ptr = node;
|
||||
|
||||
do
|
||||
{
|
||||
K max = ptr.End;
|
||||
|
||||
if (ptr.Left != null && ptr.Left.Max.CompareTo(max) > 0)
|
||||
{
|
||||
max = ptr.Left.Max;
|
||||
}
|
||||
|
||||
if (ptr.Right != null && ptr.Right.Max.CompareTo(max) > 0)
|
||||
{
|
||||
max = ptr.Right.Max;
|
||||
}
|
||||
|
||||
ptr.Max = max;
|
||||
} while ((ptr = ptr.Parent) != null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Insertion Mechanism for the interval tree. Similar to a BST insert, with the start of the range as the key.
|
||||
/// Iterates the tree starting from the root and inserts a new node where all children in the left subtree are less than <paramref name="start"/>, and all children in the right subtree are greater than <paramref name="start"/>.
|
||||
/// Each node can contain multiple values, and has an end address which is the maximum of all those values.
|
||||
/// Post insertion, the "max" value of the node and all parents are updated.
|
||||
/// </summary>
|
||||
/// <param name="start">Start of the range to insert</param>
|
||||
/// <param name="end">End of the range to insert</param>
|
||||
/// <param name="value">Value to insert</param>
|
||||
/// <param name="updateFactoryCallback">Optional factory used to create a new value if <paramref name="start"/> is already on the tree</param>
|
||||
/// <param name="outNode">Node that was inserted or modified</param>
|
||||
/// <returns>True if <paramref name="start"/> was not yet on the tree, false otherwise</returns>
|
||||
private bool BSTInsert(K start, K end, V value, Func<K, V, V> updateFactoryCallback, out IntervalTreeNode<K, V> outNode)
|
||||
{
|
||||
IntervalTreeNode<K, V> parent = null;
|
||||
IntervalTreeNode<K, V> node = _root;
|
||||
|
||||
while (node != null)
|
||||
{
|
||||
parent = node;
|
||||
int cmp = start.CompareTo(node.Start);
|
||||
if (cmp < 0)
|
||||
{
|
||||
node = node.Left;
|
||||
}
|
||||
else if (cmp > 0)
|
||||
{
|
||||
node = node.Right;
|
||||
}
|
||||
else
|
||||
{
|
||||
outNode = node;
|
||||
|
||||
if (updateFactoryCallback != null)
|
||||
{
|
||||
// Replace
|
||||
node.Value = updateFactoryCallback(start, node.Value);
|
||||
|
||||
int endCmp = end.CompareTo(node.End);
|
||||
|
||||
if (endCmp > 0)
|
||||
{
|
||||
node.End = end;
|
||||
if (end.CompareTo(node.Max) > 0)
|
||||
{
|
||||
node.Max = end;
|
||||
PropagateIncrease(node);
|
||||
RestoreBalanceAfterInsertion(node);
|
||||
}
|
||||
}
|
||||
else if (endCmp < 0)
|
||||
{
|
||||
node.End = end;
|
||||
PropagateFull(node);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
IntervalTreeNode<K, V> newNode = new IntervalTreeNode<K, V>(start, end, value, parent);
|
||||
if (newNode.Parent == null)
|
||||
{
|
||||
_root = newNode;
|
||||
}
|
||||
else if (start.CompareTo(parent.Start) < 0)
|
||||
{
|
||||
parent.Left = newNode;
|
||||
}
|
||||
else
|
||||
{
|
||||
parent.Right = newNode;
|
||||
}
|
||||
|
||||
PropagateIncrease(newNode);
|
||||
_count++;
|
||||
RestoreBalanceAfterInsertion(newNode);
|
||||
outNode = newNode;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the value from the dictionary after searching for it with <paramref name="key">.
|
||||
/// </summary>
|
||||
/// <param name="key">Tree node to be removed</param>
|
||||
private void Delete(IntervalTreeNode<K, V> nodeToDelete)
|
||||
{
|
||||
IntervalTreeNode<K, V> replacementNode;
|
||||
|
||||
if (LeftOf(nodeToDelete) == null || RightOf(nodeToDelete) == null)
|
||||
{
|
||||
replacementNode = nodeToDelete;
|
||||
}
|
||||
else
|
||||
{
|
||||
replacementNode = PredecessorOf(nodeToDelete);
|
||||
}
|
||||
|
||||
IntervalTreeNode<K, V> tmp = LeftOf(replacementNode) ?? RightOf(replacementNode);
|
||||
|
||||
if (tmp != null)
|
||||
{
|
||||
tmp.Parent = ParentOf(replacementNode);
|
||||
}
|
||||
|
||||
if (ParentOf(replacementNode) == null)
|
||||
{
|
||||
_root = tmp;
|
||||
}
|
||||
else if (replacementNode == LeftOf(ParentOf(replacementNode)))
|
||||
{
|
||||
ParentOf(replacementNode).Left = tmp;
|
||||
}
|
||||
else
|
||||
{
|
||||
ParentOf(replacementNode).Right = tmp;
|
||||
}
|
||||
|
||||
if (replacementNode != nodeToDelete)
|
||||
{
|
||||
nodeToDelete.Start = replacementNode.Start;
|
||||
nodeToDelete.Value = replacementNode.Value;
|
||||
nodeToDelete.End = replacementNode.End;
|
||||
nodeToDelete.Max = replacementNode.Max;
|
||||
}
|
||||
|
||||
PropagateFull(replacementNode);
|
||||
|
||||
if (tmp != null && ColorOf(replacementNode) == Black)
|
||||
{
|
||||
RestoreBalanceAfterRemoval(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the node with the largest key where <paramref name="node"/> is considered the root node.
|
||||
/// </summary>
|
||||
/// <param name="node">Root Node</param>
|
||||
/// <returns>Node with the maximum key in the tree of <paramref name="node"/></returns>
|
||||
private static IntervalTreeNode<K, V> Maximum(IntervalTreeNode<K, V> node)
|
||||
{
|
||||
IntervalTreeNode<K, V> tmp = node;
|
||||
while (tmp.Right != null)
|
||||
{
|
||||
tmp = tmp.Right;
|
||||
}
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds the node whose key is immediately less than <paramref name="node"/>.
|
||||
/// </summary>
|
||||
/// <param name="node">Node to find the predecessor of</param>
|
||||
/// <returns>Predecessor of <paramref name="node"/></returns>
|
||||
private static IntervalTreeNode<K, V> PredecessorOf(IntervalTreeNode<K, V> node)
|
||||
{
|
||||
if (node.Left != null)
|
||||
{
|
||||
return Maximum(node.Left);
|
||||
}
|
||||
IntervalTreeNode<K, V> parent = node.Parent;
|
||||
while (parent != null && node == parent.Left)
|
||||
{
|
||||
node = parent;
|
||||
parent = parent.Parent;
|
||||
}
|
||||
return parent;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods (RBL)
|
||||
|
||||
private void RestoreBalanceAfterRemoval(IntervalTreeNode<K, V> balanceNode)
|
||||
{
|
||||
IntervalTreeNode<K, V> ptr = balanceNode;
|
||||
|
||||
while (ptr != _root && ColorOf(ptr) == Black)
|
||||
{
|
||||
if (ptr == LeftOf(ParentOf(ptr)))
|
||||
{
|
||||
IntervalTreeNode<K, V> sibling = RightOf(ParentOf(ptr));
|
||||
|
||||
if (ColorOf(sibling) == Red)
|
||||
{
|
||||
SetColor(sibling, Black);
|
||||
SetColor(ParentOf(ptr), Red);
|
||||
RotateLeft(ParentOf(ptr));
|
||||
sibling = RightOf(ParentOf(ptr));
|
||||
}
|
||||
if (ColorOf(LeftOf(sibling)) == Black && ColorOf(RightOf(sibling)) == Black)
|
||||
{
|
||||
SetColor(sibling, Red);
|
||||
ptr = ParentOf(ptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ColorOf(RightOf(sibling)) == Black)
|
||||
{
|
||||
SetColor(LeftOf(sibling), Black);
|
||||
SetColor(sibling, Red);
|
||||
RotateRight(sibling);
|
||||
sibling = RightOf(ParentOf(ptr));
|
||||
}
|
||||
SetColor(sibling, ColorOf(ParentOf(ptr)));
|
||||
SetColor(ParentOf(ptr), Black);
|
||||
SetColor(RightOf(sibling), Black);
|
||||
RotateLeft(ParentOf(ptr));
|
||||
ptr = _root;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
IntervalTreeNode<K, V> sibling = LeftOf(ParentOf(ptr));
|
||||
|
||||
if (ColorOf(sibling) == Red)
|
||||
{
|
||||
SetColor(sibling, Black);
|
||||
SetColor(ParentOf(ptr), Red);
|
||||
RotateRight(ParentOf(ptr));
|
||||
sibling = LeftOf(ParentOf(ptr));
|
||||
}
|
||||
if (ColorOf(RightOf(sibling)) == Black && ColorOf(LeftOf(sibling)) == Black)
|
||||
{
|
||||
SetColor(sibling, Red);
|
||||
ptr = ParentOf(ptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ColorOf(LeftOf(sibling)) == Black)
|
||||
{
|
||||
SetColor(RightOf(sibling), Black);
|
||||
SetColor(sibling, Red);
|
||||
RotateLeft(sibling);
|
||||
sibling = LeftOf(ParentOf(ptr));
|
||||
}
|
||||
SetColor(sibling, ColorOf(ParentOf(ptr)));
|
||||
SetColor(ParentOf(ptr), Black);
|
||||
SetColor(LeftOf(sibling), Black);
|
||||
RotateRight(ParentOf(ptr));
|
||||
ptr = _root;
|
||||
}
|
||||
}
|
||||
}
|
||||
SetColor(ptr, Black);
|
||||
}
|
||||
|
||||
private void RestoreBalanceAfterInsertion(IntervalTreeNode<K, V> balanceNode)
|
||||
{
|
||||
SetColor(balanceNode, Red);
|
||||
while (balanceNode != null && balanceNode != _root && ColorOf(ParentOf(balanceNode)) == Red)
|
||||
{
|
||||
if (ParentOf(balanceNode) == LeftOf(ParentOf(ParentOf(balanceNode))))
|
||||
{
|
||||
IntervalTreeNode<K, V> sibling = RightOf(ParentOf(ParentOf(balanceNode)));
|
||||
|
||||
if (ColorOf(sibling) == Red)
|
||||
{
|
||||
SetColor(ParentOf(balanceNode), Black);
|
||||
SetColor(sibling, Black);
|
||||
SetColor(ParentOf(ParentOf(balanceNode)), Red);
|
||||
balanceNode = ParentOf(ParentOf(balanceNode));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (balanceNode == RightOf(ParentOf(balanceNode)))
|
||||
{
|
||||
balanceNode = ParentOf(balanceNode);
|
||||
RotateLeft(balanceNode);
|
||||
}
|
||||
SetColor(ParentOf(balanceNode), Black);
|
||||
SetColor(ParentOf(ParentOf(balanceNode)), Red);
|
||||
RotateRight(ParentOf(ParentOf(balanceNode)));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
IntervalTreeNode<K, V> sibling = LeftOf(ParentOf(ParentOf(balanceNode)));
|
||||
|
||||
if (ColorOf(sibling) == Red)
|
||||
{
|
||||
SetColor(ParentOf(balanceNode), Black);
|
||||
SetColor(sibling, Black);
|
||||
SetColor(ParentOf(ParentOf(balanceNode)), Red);
|
||||
balanceNode = ParentOf(ParentOf(balanceNode));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (balanceNode == LeftOf(ParentOf(balanceNode)))
|
||||
{
|
||||
balanceNode = ParentOf(balanceNode);
|
||||
RotateRight(balanceNode);
|
||||
}
|
||||
SetColor(ParentOf(balanceNode), Black);
|
||||
SetColor(ParentOf(ParentOf(balanceNode)), Red);
|
||||
RotateLeft(ParentOf(ParentOf(balanceNode)));
|
||||
}
|
||||
}
|
||||
}
|
||||
SetColor(_root, Black);
|
||||
}
|
||||
|
||||
private void RotateLeft(IntervalTreeNode<K, V> node)
|
||||
{
|
||||
if (node != null)
|
||||
{
|
||||
IntervalTreeNode<K, V> right = RightOf(node);
|
||||
node.Right = LeftOf(right);
|
||||
if (node.Right != null)
|
||||
{
|
||||
node.Right.Parent = node;
|
||||
}
|
||||
IntervalTreeNode<K, V> nodeParent = ParentOf(node);
|
||||
right.Parent = nodeParent;
|
||||
if (nodeParent == null)
|
||||
{
|
||||
_root = right;
|
||||
}
|
||||
else if (node == LeftOf(nodeParent))
|
||||
{
|
||||
nodeParent.Left = right;
|
||||
}
|
||||
else
|
||||
{
|
||||
nodeParent.Right = right;
|
||||
}
|
||||
right.Left = node;
|
||||
node.Parent = right;
|
||||
|
||||
PropagateFull(node);
|
||||
}
|
||||
}
|
||||
|
||||
private void RotateRight(IntervalTreeNode<K, V> node)
|
||||
{
|
||||
if (node != null)
|
||||
{
|
||||
IntervalTreeNode<K, V> left = LeftOf(node);
|
||||
node.Left = RightOf(left);
|
||||
if (node.Left != null)
|
||||
{
|
||||
node.Left.Parent = node;
|
||||
}
|
||||
IntervalTreeNode<K, V> nodeParent = ParentOf(node);
|
||||
left.Parent = nodeParent;
|
||||
if (nodeParent == null)
|
||||
{
|
||||
_root = left;
|
||||
}
|
||||
else if (node == RightOf(nodeParent))
|
||||
{
|
||||
nodeParent.Right = left;
|
||||
}
|
||||
else
|
||||
{
|
||||
nodeParent.Left = left;
|
||||
}
|
||||
left.Right = node;
|
||||
node.Parent = left;
|
||||
|
||||
PropagateFull(node);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Safety-Methods
|
||||
|
||||
// These methods save memory by allowing us to forego sentinel nil nodes, as well as serve as protection against NullReferenceExceptions.
|
||||
|
||||
/// <summary>
|
||||
/// Returns the color of <paramref name="node"/>, or Black if it is null.
|
||||
/// </summary>
|
||||
/// <param name="node">Node</param>
|
||||
/// <returns>The boolean color of <paramref name="node"/>, or black if null</returns>
|
||||
private static bool ColorOf(IntervalTreeNode<K, V> node)
|
||||
{
|
||||
return node == null || node.Color;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the color of <paramref name="node"/> node to <paramref name="color"/>.
|
||||
/// <br></br>
|
||||
/// This method does nothing if <paramref name="node"/> is null.
|
||||
/// </summary>
|
||||
/// <param name="node">Node to set the color of</param>
|
||||
/// <param name="color">Color (Boolean)</param>
|
||||
private static void SetColor(IntervalTreeNode<K, V> node, bool color)
|
||||
{
|
||||
if (node != null)
|
||||
{
|
||||
node.Color = color;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method returns the left node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
|
||||
/// </summary>
|
||||
/// <param name="node">Node to retrieve the left child from</param>
|
||||
/// <returns>Left child of <paramref name="node"/></returns>
|
||||
private static IntervalTreeNode<K, V> LeftOf(IntervalTreeNode<K, V> node)
|
||||
{
|
||||
return node?.Left;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method returns the right node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
|
||||
/// </summary>
|
||||
/// <param name="node">Node to retrieve the right child from</param>
|
||||
/// <returns>Right child of <paramref name="node"/></returns>
|
||||
private static IntervalTreeNode<K, V> RightOf(IntervalTreeNode<K, V> node)
|
||||
{
|
||||
return node?.Right;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the parent node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
|
||||
/// </summary>
|
||||
/// <param name="node">Node to retrieve the parent from</param>
|
||||
/// <returns>Parent of <paramref name="node"/></returns>
|
||||
private static IntervalTreeNode<K, V> ParentOf(IntervalTreeNode<K, V> node)
|
||||
{
|
||||
return node?.Parent;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public bool ContainsKey(K key)
|
||||
{
|
||||
return GetNode(key) != null;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_root = null;
|
||||
_count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a node in the IntervalTree which contains start and end keys of type K, and a value of generic type V.
|
||||
/// </summary>
|
||||
/// <typeparam name="K">Key type of the node</typeparam>
|
||||
/// <typeparam name="V">Value type of the node</typeparam>
|
||||
class IntervalTreeNode<K, V>
|
||||
{
|
||||
public bool Color = true;
|
||||
public IntervalTreeNode<K, V> Left = null;
|
||||
public IntervalTreeNode<K, V> Right = null;
|
||||
public IntervalTreeNode<K, V> Parent = null;
|
||||
|
||||
/// <summary>
|
||||
/// The start of the range.
|
||||
/// </summary>
|
||||
public K Start;
|
||||
|
||||
/// <summary>
|
||||
/// The end of the range.
|
||||
/// </summary>
|
||||
public K End;
|
||||
|
||||
/// <summary>
|
||||
/// The maximum end value of this node and all its children.
|
||||
/// </summary>
|
||||
public K Max;
|
||||
|
||||
/// <summary>
|
||||
/// Value stored on this node.
|
||||
/// </summary>
|
||||
public V Value;
|
||||
|
||||
public IntervalTreeNode(K start, K end, V value, IntervalTreeNode<K, V> parent)
|
||||
{
|
||||
Start = start;
|
||||
End = end;
|
||||
Max = end;
|
||||
Value = value;
|
||||
Parent = parent;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,293 +0,0 @@
|
||||
using Ryujinx.Memory.Range;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Ryujinx.Memory.WindowsShared
|
||||
{
|
||||
/// <summary>
|
||||
/// A specialized list used for keeping track of Windows 10's memory placeholders.
|
||||
/// This is used to make splitting a large placeholder into equally small
|
||||
/// granular chunks much easier, while avoiding slowdown due to a large number of
|
||||
/// placeholders by coalescing adjacent granular placeholders after they are unused.
|
||||
/// </summary>
|
||||
class PlaceholderList
|
||||
{
|
||||
private class PlaceholderBlock : IRange
|
||||
{
|
||||
public ulong Address { get; }
|
||||
public ulong Size { get; private set; }
|
||||
public ulong EndAddress { get; private set; }
|
||||
public bool IsGranular { get; set; }
|
||||
|
||||
public PlaceholderBlock(ulong id, ulong size, bool isGranular)
|
||||
{
|
||||
Address = id;
|
||||
Size = size;
|
||||
EndAddress = id + size;
|
||||
IsGranular = isGranular;
|
||||
}
|
||||
|
||||
public bool OverlapsWith(ulong address, ulong size)
|
||||
{
|
||||
return Address < address + size && address < EndAddress;
|
||||
}
|
||||
|
||||
public void ExtendTo(ulong end, RangeList<PlaceholderBlock> list)
|
||||
{
|
||||
EndAddress = end;
|
||||
Size = end - Address;
|
||||
|
||||
list.UpdateEndAddress(this);
|
||||
}
|
||||
}
|
||||
|
||||
private RangeList<PlaceholderBlock> _placeholders;
|
||||
private PlaceholderBlock[] _foundBlocks = new PlaceholderBlock[32];
|
||||
|
||||
/// <summary>
|
||||
/// Create a new list to manage placeholders.
|
||||
/// Note that a size is measured in granular placeholders.
|
||||
/// If the placeholder granularity is 65536 bytes, then a 65536 region will be covered by 1 placeholder granularity.
|
||||
/// </summary>
|
||||
/// <param name="size">Size measured in granular placeholders</param>
|
||||
public PlaceholderList(ulong size)
|
||||
{
|
||||
_placeholders = new RangeList<PlaceholderBlock>();
|
||||
|
||||
_placeholders.Add(new PlaceholderBlock(0, size, false));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensure that the given range of placeholders is granular.
|
||||
/// </summary>
|
||||
/// <param name="id">Start of the range, measured in granular placeholders</param>
|
||||
/// <param name="size">Size of the range, measured in granular placeholders</param>
|
||||
/// <param name="splitPlaceholderCallback">Callback function to run when splitting placeholders, calls with (start, middle)</param>
|
||||
public void EnsurePlaceholders(ulong id, ulong size, Action<ulong, ulong> splitPlaceholderCallback)
|
||||
{
|
||||
// Search 1 before and after the placeholders, as we may need to expand/join granular regions surrounding the requested area.
|
||||
|
||||
ulong endId = id + size;
|
||||
ulong searchStartId = id == 0 ? 0 : (id - 1);
|
||||
int blockCount = _placeholders.FindOverlapsNonOverlapping(searchStartId, (endId - searchStartId) + 1, ref _foundBlocks);
|
||||
|
||||
PlaceholderBlock first = _foundBlocks[0];
|
||||
PlaceholderBlock last = _foundBlocks[blockCount - 1];
|
||||
bool overlapStart = first.EndAddress >= id && id != 0;
|
||||
bool overlapEnd = last.Address <= endId;
|
||||
|
||||
for (int i = 0; i < blockCount; i++)
|
||||
{
|
||||
// Go through all non-granular blocks in the range and create placeholders.
|
||||
PlaceholderBlock block = _foundBlocks[i];
|
||||
|
||||
if (block.Address <= id && block.EndAddress >= endId && block.IsGranular)
|
||||
{
|
||||
return; // The region we're searching for is already granular.
|
||||
}
|
||||
|
||||
if (!block.IsGranular)
|
||||
{
|
||||
ulong placeholderStart = Math.Max(block.Address, id);
|
||||
ulong placeholderEnd = Math.Min(block.EndAddress - 1, endId);
|
||||
|
||||
if (placeholderStart != block.Address && placeholderStart != block.EndAddress)
|
||||
{
|
||||
splitPlaceholderCallback(block.Address, placeholderStart - block.Address);
|
||||
}
|
||||
|
||||
for (ulong j = placeholderStart; j < placeholderEnd; j++)
|
||||
{
|
||||
splitPlaceholderCallback(j, 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (!((block == first && overlapStart) || (block == last && overlapEnd)))
|
||||
{
|
||||
// Remove blocks that will be replaced
|
||||
_placeholders.Remove(block);
|
||||
}
|
||||
}
|
||||
|
||||
if (overlapEnd)
|
||||
{
|
||||
if (!(first == last && overlapStart))
|
||||
{
|
||||
_placeholders.Remove(last);
|
||||
}
|
||||
|
||||
if (last.IsGranular)
|
||||
{
|
||||
endId = last.EndAddress;
|
||||
}
|
||||
else if (last.EndAddress != endId)
|
||||
{
|
||||
_placeholders.Add(new PlaceholderBlock(endId, last.EndAddress - endId, false));
|
||||
}
|
||||
}
|
||||
|
||||
if (overlapStart && first.IsGranular)
|
||||
{
|
||||
first.ExtendTo(endId, _placeholders);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (overlapStart)
|
||||
{
|
||||
first.ExtendTo(id, _placeholders);
|
||||
}
|
||||
|
||||
_placeholders.Add(new PlaceholderBlock(id, endId - id, true));
|
||||
}
|
||||
|
||||
ValidateList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Coalesces placeholders in a given region, as they are not being used.
|
||||
/// This assumes that the region only contains placeholders - all views and allocations must have been replaced with placeholders.
|
||||
/// </summary>
|
||||
/// <param name="id">Start of the range, measured in granular placeholders</param>
|
||||
/// <param name="size">Size of the range, measured in granular placeholders</param>
|
||||
/// <param name="coalescePlaceholderCallback">Callback function to run when coalescing two placeholders, calls with (start, end)</param>
|
||||
public void RemovePlaceholders(ulong id, ulong size, Action<ulong, ulong> coalescePlaceholderCallback)
|
||||
{
|
||||
ulong endId = id + size;
|
||||
int blockCount = _placeholders.FindOverlapsNonOverlapping(id, size, ref _foundBlocks);
|
||||
|
||||
PlaceholderBlock first = _foundBlocks[0];
|
||||
PlaceholderBlock last = _foundBlocks[blockCount - 1];
|
||||
|
||||
// All granular blocks must have non-granular blocks surrounding them, unless they start at 0.
|
||||
// We must extend the non-granular blocks into the granular ones. This does mean that we need to search twice.
|
||||
|
||||
if (first.IsGranular || last.IsGranular)
|
||||
{
|
||||
ulong surroundStart = Math.Max(0, (first.IsGranular && first.Address != 0) ? first.Address - 1 : id);
|
||||
blockCount = _placeholders.FindOverlapsNonOverlapping(
|
||||
surroundStart,
|
||||
(last.IsGranular ? last.EndAddress + 1 : endId) - surroundStart,
|
||||
ref _foundBlocks);
|
||||
|
||||
first = _foundBlocks[0];
|
||||
last = _foundBlocks[blockCount - 1];
|
||||
}
|
||||
|
||||
if (first == last)
|
||||
{
|
||||
return; // Already coalesced.
|
||||
}
|
||||
|
||||
PlaceholderBlock extendBlock = id == 0 ? null : first;
|
||||
bool newBlock = false;
|
||||
for (int i = extendBlock == null ? 0 : 1; i < blockCount; i++)
|
||||
{
|
||||
// Go through all granular blocks in the range and extend placeholders.
|
||||
PlaceholderBlock block = _foundBlocks[i];
|
||||
|
||||
ulong blockEnd = block.EndAddress;
|
||||
ulong extendFrom;
|
||||
ulong extent = Math.Min(blockEnd, endId);
|
||||
|
||||
if (block.Address < id && blockEnd > id)
|
||||
{
|
||||
block.ExtendTo(id, _placeholders);
|
||||
extendBlock = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
_placeholders.Remove(block);
|
||||
}
|
||||
|
||||
if (extendBlock == null)
|
||||
{
|
||||
extendFrom = id;
|
||||
extendBlock = new PlaceholderBlock(id, extent - id, false);
|
||||
_placeholders.Add(extendBlock);
|
||||
|
||||
if (blockEnd > extent)
|
||||
{
|
||||
_placeholders.Add(new PlaceholderBlock(extent, blockEnd - extent, true));
|
||||
|
||||
// Skip the next non-granular block, and extend from that into the granular block afterwards.
|
||||
// (assuming that one is still in the requested range)
|
||||
|
||||
if (i + 1 < blockCount)
|
||||
{
|
||||
extendBlock = _foundBlocks[i + 1];
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
newBlock = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
extendFrom = extendBlock.Address;
|
||||
extendBlock.ExtendTo(block.IsGranular ? extent : block.EndAddress, _placeholders);
|
||||
}
|
||||
|
||||
if (block.IsGranular)
|
||||
{
|
||||
ulong placeholderStart = Math.Max(block.Address, id);
|
||||
ulong placeholderEnd = extent;
|
||||
|
||||
if (newBlock)
|
||||
{
|
||||
placeholderStart++;
|
||||
newBlock = false;
|
||||
}
|
||||
|
||||
for (ulong j = placeholderStart; j < placeholderEnd; j++)
|
||||
{
|
||||
coalescePlaceholderCallback(extendFrom, (j + 1) - extendFrom);
|
||||
}
|
||||
|
||||
if (extent < block.EndAddress)
|
||||
{
|
||||
_placeholders.Add(new PlaceholderBlock(placeholderEnd, block.EndAddress - placeholderEnd, true));
|
||||
ValidateList();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
coalescePlaceholderCallback(extendFrom, block.EndAddress - extendFrom);
|
||||
}
|
||||
}
|
||||
|
||||
ValidateList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensure that the placeholder list is valid.
|
||||
/// A valid list should not have any gaps between the placeholders,
|
||||
/// and there may be no placehonders with the same IsGranular value next to each other.
|
||||
/// </summary>
|
||||
[Conditional("DEBUG")]
|
||||
private void ValidateList()
|
||||
{
|
||||
bool isGranular = false;
|
||||
bool first = true;
|
||||
ulong lastAddress = 0;
|
||||
|
||||
foreach (var placeholder in _placeholders)
|
||||
{
|
||||
if (placeholder.Address != lastAddress)
|
||||
{
|
||||
throw new InvalidOperationException("Gap in placeholder list.");
|
||||
}
|
||||
|
||||
if (isGranular == placeholder.IsGranular && !first)
|
||||
{
|
||||
throw new InvalidOperationException("Placeholder list not alternating.");
|
||||
}
|
||||
|
||||
first = false;
|
||||
isGranular = placeholder.IsGranular;
|
||||
lastAddress = placeholder.EndAddress;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
636
Ryujinx.Memory/WindowsShared/PlaceholderManager.cs
Normal file
636
Ryujinx.Memory/WindowsShared/PlaceholderManager.cs
Normal file
@ -0,0 +1,636 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.Versioning;
|
||||
using System.Threading;
|
||||
|
||||
namespace Ryujinx.Memory.WindowsShared
|
||||
{
|
||||
/// <summary>
|
||||
/// Windows memory placeholder manager.
|
||||
/// </summary>
|
||||
[SupportedOSPlatform("windows")]
|
||||
class PlaceholderManager
|
||||
{
|
||||
private const ulong MinimumPageSize = 0x1000;
|
||||
|
||||
[ThreadStatic]
|
||||
private static int _threadLocalPartialUnmapsCount;
|
||||
|
||||
private readonly IntervalTree<ulong, ulong> _mappings;
|
||||
private readonly IntervalTree<ulong, MemoryPermission> _protections;
|
||||
private readonly ReaderWriterLock _partialUnmapLock;
|
||||
private int _partialUnmapsCount;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the Windows memory placeholder manager.
|
||||
/// </summary>
|
||||
public PlaceholderManager()
|
||||
{
|
||||
_mappings = new IntervalTree<ulong, ulong>();
|
||||
_protections = new IntervalTree<ulong, MemoryPermission>();
|
||||
_partialUnmapLock = new ReaderWriterLock();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reserves a range of the address space to be later mapped as shared memory views.
|
||||
/// </summary>
|
||||
/// <param name="address">Start address of the region to reserve</param>
|
||||
/// <param name="size">Size in bytes of the region to reserve</param>
|
||||
public void ReserveRange(ulong address, ulong size)
|
||||
{
|
||||
lock (_mappings)
|
||||
{
|
||||
_mappings.Add(address, address + size, ulong.MaxValue);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps a shared memory view on a previously reserved memory region.
|
||||
/// </summary>
|
||||
/// <param name="sharedMemory">Shared memory that will be the backing storage for the view</param>
|
||||
/// <param name="srcOffset">Offset in the shared memory to map</param>
|
||||
/// <param name="location">Address to map the view into</param>
|
||||
/// <param name="size">Size of the view in bytes</param>
|
||||
public void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size)
|
||||
{
|
||||
_partialUnmapLock.AcquireReaderLock(Timeout.Infinite);
|
||||
|
||||
try
|
||||
{
|
||||
UnmapViewInternal(sharedMemory, location, size);
|
||||
MapViewInternal(sharedMemory, srcOffset, location, size);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_partialUnmapLock.ReleaseReaderLock();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps a shared memory view on a previously reserved memory region.
|
||||
/// </summary>
|
||||
/// <param name="sharedMemory">Shared memory that will be the backing storage for the view</param>
|
||||
/// <param name="srcOffset">Offset in the shared memory to map</param>
|
||||
/// <param name="location">Address to map the view into</param>
|
||||
/// <param name="size">Size of the view in bytes</param>
|
||||
/// <exception cref="WindowsApiException">Thrown when the Windows API returns an error mapping the memory</exception>
|
||||
private void MapViewInternal(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size)
|
||||
{
|
||||
SplitForMap((ulong)location, (ulong)size, srcOffset);
|
||||
|
||||
var ptr = WindowsApi.MapViewOfFile3(
|
||||
sharedMemory,
|
||||
WindowsApi.CurrentProcessHandle,
|
||||
location,
|
||||
srcOffset,
|
||||
size,
|
||||
0x4000,
|
||||
MemoryProtection.ReadWrite,
|
||||
IntPtr.Zero,
|
||||
0);
|
||||
|
||||
if (ptr == IntPtr.Zero)
|
||||
{
|
||||
throw new WindowsApiException("MapViewOfFile3");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Splits a larger placeholder, slicing at the start and end address, for a new memory mapping.
|
||||
/// </summary>
|
||||
/// <param name="address">Address to split</param>
|
||||
/// <param name="size">Size of the new region</param>
|
||||
/// <param name="backingOffset">Offset in the shared memory that will be mapped</param>
|
||||
private void SplitForMap(ulong address, ulong size, ulong backingOffset)
|
||||
{
|
||||
ulong endAddress = address + size;
|
||||
|
||||
var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
|
||||
|
||||
lock (_mappings)
|
||||
{
|
||||
int count = _mappings.Get(address, endAddress, ref overlaps);
|
||||
|
||||
Debug.Assert(count == 1);
|
||||
Debug.Assert(!IsMapped(overlaps[0].Value));
|
||||
|
||||
var overlap = overlaps[0];
|
||||
|
||||
// Tree operations might modify the node start/end values, so save a copy before we modify the tree.
|
||||
ulong overlapStart = overlap.Start;
|
||||
ulong overlapEnd = overlap.End;
|
||||
ulong overlapValue = overlap.Value;
|
||||
|
||||
_mappings.Remove(overlap);
|
||||
|
||||
bool overlapStartsBefore = overlapStart < address;
|
||||
bool overlapEndsAfter = overlapEnd > endAddress;
|
||||
|
||||
if (overlapStartsBefore && overlapEndsAfter)
|
||||
{
|
||||
CheckFreeResult(WindowsApi.VirtualFree(
|
||||
(IntPtr)address,
|
||||
(IntPtr)size,
|
||||
AllocationType.Release | AllocationType.PreservePlaceholder));
|
||||
|
||||
_mappings.Add(overlapStart, address, overlapValue);
|
||||
_mappings.Add(endAddress, overlapEnd, AddBackingOffset(overlapValue, endAddress - overlapStart));
|
||||
}
|
||||
else if (overlapStartsBefore)
|
||||
{
|
||||
ulong overlappedSize = overlapEnd - address;
|
||||
|
||||
CheckFreeResult(WindowsApi.VirtualFree(
|
||||
(IntPtr)address,
|
||||
(IntPtr)overlappedSize,
|
||||
AllocationType.Release | AllocationType.PreservePlaceholder));
|
||||
|
||||
_mappings.Add(overlapStart, address, overlapValue);
|
||||
}
|
||||
else if (overlapEndsAfter)
|
||||
{
|
||||
ulong overlappedSize = endAddress - overlapStart;
|
||||
|
||||
CheckFreeResult(WindowsApi.VirtualFree(
|
||||
(IntPtr)overlapStart,
|
||||
(IntPtr)overlappedSize,
|
||||
AllocationType.Release | AllocationType.PreservePlaceholder));
|
||||
|
||||
_mappings.Add(endAddress, overlapEnd, AddBackingOffset(overlapValue, overlappedSize));
|
||||
}
|
||||
|
||||
_mappings.Add(address, endAddress, backingOffset);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unmaps a view that has been previously mapped with <see cref="MapView"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// For "partial unmaps" (when not the entire mapped range is being unmapped), it might be
|
||||
/// necessary to unmap the whole range and then remap the sub-ranges that should remain mapped.
|
||||
/// </remarks>
|
||||
/// <param name="sharedMemory">Shared memory that the view being unmapped belongs to</param>
|
||||
/// <param name="location">Address to unmap</param>
|
||||
/// <param name="size">Size of the region to unmap in bytes</param>
|
||||
public void UnmapView(IntPtr sharedMemory, IntPtr location, IntPtr size)
|
||||
{
|
||||
_partialUnmapLock.AcquireReaderLock(Timeout.Infinite);
|
||||
|
||||
try
|
||||
{
|
||||
UnmapViewInternal(sharedMemory, location, size);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_partialUnmapLock.ReleaseReaderLock();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unmaps a view that has been previously mapped with <see cref="MapView"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// For "partial unmaps" (when not the entire mapped range is being unmapped), it might be
|
||||
/// necessary to unmap the whole range and then remap the sub-ranges that should remain mapped.
|
||||
/// </remarks>
|
||||
/// <param name="sharedMemory">Shared memory that the view being unmapped belongs to</param>
|
||||
/// <param name="location">Address to unmap</param>
|
||||
/// <param name="size">Size of the region to unmap in bytes</param>
|
||||
/// <exception cref="WindowsApiException">Thrown when the Windows API returns an error unmapping or remapping the memory</exception>
|
||||
private void UnmapViewInternal(IntPtr sharedMemory, IntPtr location, IntPtr size)
|
||||
{
|
||||
ulong startAddress = (ulong)location;
|
||||
ulong unmapSize = (ulong)size;
|
||||
ulong endAddress = startAddress + unmapSize;
|
||||
|
||||
var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
|
||||
int count;
|
||||
|
||||
lock (_mappings)
|
||||
{
|
||||
count = _mappings.Get(startAddress, endAddress, ref overlaps);
|
||||
}
|
||||
|
||||
for (int index = 0; index < count; index++)
|
||||
{
|
||||
var overlap = overlaps[index];
|
||||
|
||||
if (IsMapped(overlap.Value))
|
||||
{
|
||||
if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)overlap.Start, 2))
|
||||
{
|
||||
throw new WindowsApiException("UnmapViewOfFile2");
|
||||
}
|
||||
|
||||
// Tree operations might modify the node start/end values, so save a copy before we modify the tree.
|
||||
ulong overlapStart = overlap.Start;
|
||||
ulong overlapEnd = overlap.End;
|
||||
ulong overlapValue = overlap.Value;
|
||||
|
||||
lock (_mappings)
|
||||
{
|
||||
_mappings.Remove(overlap);
|
||||
_mappings.Add(overlapStart, overlapEnd, ulong.MaxValue);
|
||||
}
|
||||
|
||||
bool overlapStartsBefore = overlapStart < startAddress;
|
||||
bool overlapEndsAfter = overlapEnd > endAddress;
|
||||
|
||||
if (overlapStartsBefore || overlapEndsAfter)
|
||||
{
|
||||
// If the overlap extends beyond the region we are unmapping,
|
||||
// then we need to re-map the regions that are supposed to remain mapped.
|
||||
// This is necessary because Windows does not support partial view unmaps.
|
||||
// That is, you can only fully unmap a view that was previously mapped, you can't just unmap a chunck of it.
|
||||
|
||||
LockCookie lockCookie = _partialUnmapLock.UpgradeToWriterLock(Timeout.Infinite);
|
||||
|
||||
_partialUnmapsCount++;
|
||||
|
||||
if (overlapStartsBefore)
|
||||
{
|
||||
ulong remapSize = startAddress - overlapStart;
|
||||
|
||||
MapViewInternal(sharedMemory, overlapValue, (IntPtr)overlapStart, (IntPtr)remapSize);
|
||||
RestoreRangeProtection(overlapStart, remapSize);
|
||||
}
|
||||
|
||||
if (overlapEndsAfter)
|
||||
{
|
||||
ulong overlappedSize = endAddress - overlapStart;
|
||||
ulong remapBackingOffset = overlapValue + overlappedSize;
|
||||
ulong remapAddress = overlapStart + overlappedSize;
|
||||
ulong remapSize = overlapEnd - endAddress;
|
||||
|
||||
MapViewInternal(sharedMemory, remapBackingOffset, (IntPtr)remapAddress, (IntPtr)remapSize);
|
||||
RestoreRangeProtection(remapAddress, remapSize);
|
||||
}
|
||||
|
||||
_partialUnmapLock.DowngradeFromWriterLock(ref lockCookie);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CoalesceForUnmap(startAddress, unmapSize);
|
||||
RemoveProtection(startAddress, unmapSize);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Coalesces adjacent placeholders after unmap.
|
||||
/// </summary>
|
||||
/// <param name="address">Address of the region that was unmapped</param>
|
||||
/// <param name="size">Size of the region that was unmapped in bytes</param>
|
||||
private void CoalesceForUnmap(ulong address, ulong size)
|
||||
{
|
||||
ulong endAddress = address + size;
|
||||
var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
|
||||
int unmappedCount = 0;
|
||||
|
||||
lock (_mappings)
|
||||
{
|
||||
int count = _mappings.Get(address - MinimumPageSize, endAddress + MinimumPageSize, ref overlaps);
|
||||
if (count < 2)
|
||||
{
|
||||
// Nothing to coalesce if we only have 1 or no overlaps.
|
||||
return;
|
||||
}
|
||||
|
||||
for (int index = 0; index < count; index++)
|
||||
{
|
||||
var overlap = overlaps[index];
|
||||
|
||||
if (!IsMapped(overlap.Value))
|
||||
{
|
||||
if (address > overlap.Start)
|
||||
{
|
||||
address = overlap.Start;
|
||||
}
|
||||
|
||||
if (endAddress < overlap.End)
|
||||
{
|
||||
endAddress = overlap.End;
|
||||
}
|
||||
|
||||
_mappings.Remove(overlap);
|
||||
|
||||
unmappedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
_mappings.Add(address, endAddress, ulong.MaxValue);
|
||||
}
|
||||
|
||||
if (unmappedCount > 1)
|
||||
{
|
||||
size = endAddress - address;
|
||||
|
||||
CheckFreeResult(WindowsApi.VirtualFree(
|
||||
(IntPtr)address,
|
||||
(IntPtr)size,
|
||||
AllocationType.Release | AllocationType.CoalescePlaceholders));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reprotects a region of memory that has been mapped.
|
||||
/// </summary>
|
||||
/// <param name="address">Address of the region to reprotect</param>
|
||||
/// <param name="size">Size of the region to reprotect in bytes</param>
|
||||
/// <param name="permission">New permissions</param>
|
||||
/// <returns>True if the reprotection was successful, false otherwise</returns>
|
||||
public bool ReprotectView(IntPtr address, IntPtr size, MemoryPermission permission)
|
||||
{
|
||||
_partialUnmapLock.AcquireReaderLock(Timeout.Infinite);
|
||||
|
||||
try
|
||||
{
|
||||
return ReprotectViewInternal(address, size, permission, false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_partialUnmapLock.ReleaseReaderLock();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reprotects a region of memory that has been mapped.
|
||||
/// </summary>
|
||||
/// <param name="address">Address of the region to reprotect</param>
|
||||
/// <param name="size">Size of the region to reprotect in bytes</param>
|
||||
/// <param name="permission">New permissions</param>
|
||||
/// <param name="throwOnError">Throw an exception instead of returning an error if the operation fails</param>
|
||||
/// <returns>True if the reprotection was successful or if <paramref name="throwOnError"/> is true, false otherwise</returns>
|
||||
/// <exception cref="WindowsApiException">If <paramref name="throwOnError"/> is true, it is thrown when the Windows API returns an error reprotecting the memory</exception>
|
||||
private bool ReprotectViewInternal(IntPtr address, IntPtr size, MemoryPermission permission, bool throwOnError)
|
||||
{
|
||||
ulong reprotectAddress = (ulong)address;
|
||||
ulong reprotectSize = (ulong)size;
|
||||
ulong endAddress = reprotectAddress + reprotectSize;
|
||||
|
||||
var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
|
||||
int count;
|
||||
|
||||
lock (_mappings)
|
||||
{
|
||||
count = _mappings.Get(reprotectAddress, endAddress, ref overlaps);
|
||||
}
|
||||
|
||||
bool success = true;
|
||||
|
||||
for (int index = 0; index < count; index++)
|
||||
{
|
||||
var overlap = overlaps[index];
|
||||
|
||||
ulong mappedAddress = overlap.Start;
|
||||
ulong mappedSize = overlap.End - overlap.Start;
|
||||
|
||||
if (mappedAddress < reprotectAddress)
|
||||
{
|
||||
ulong delta = reprotectAddress - mappedAddress;
|
||||
mappedAddress = reprotectAddress;
|
||||
mappedSize -= delta;
|
||||
}
|
||||
|
||||
ulong mappedEndAddress = mappedAddress + mappedSize;
|
||||
|
||||
if (mappedEndAddress > endAddress)
|
||||
{
|
||||
ulong delta = mappedEndAddress - endAddress;
|
||||
mappedSize -= delta;
|
||||
}
|
||||
|
||||
if (!WindowsApi.VirtualProtect((IntPtr)mappedAddress, (IntPtr)mappedSize, WindowsApi.GetProtection(permission), out _))
|
||||
{
|
||||
if (throwOnError)
|
||||
{
|
||||
throw new WindowsApiException("VirtualProtect");
|
||||
}
|
||||
|
||||
success = false;
|
||||
}
|
||||
|
||||
// We only keep track of "non-standard" protections,
|
||||
// that is, everything that is not just RW (which is the default when views are mapped).
|
||||
if (permission == MemoryPermission.ReadAndWrite)
|
||||
{
|
||||
RemoveProtection(mappedAddress, mappedSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
AddProtection(mappedAddress, mappedSize, permission);
|
||||
}
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks the result of a VirtualFree operation, throwing if needed.
|
||||
/// </summary>
|
||||
/// <param name="success">Operation result</param>
|
||||
/// <exception cref="WindowsApiException">Thrown if <paramref name="success"/> is false</exception>
|
||||
private static void CheckFreeResult(bool success)
|
||||
{
|
||||
if (!success)
|
||||
{
|
||||
throw new WindowsApiException("VirtualFree");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds an offset to a backing offset. This will do nothing if the backing offset is the special "unmapped" value.
|
||||
/// </summary>
|
||||
/// <param name="backingOffset">Backing offset</param>
|
||||
/// <param name="offset">Offset to be added</param>
|
||||
/// <returns>Added offset or just <paramref name="backingOffset"/> if the region is unmapped</returns>
|
||||
private static ulong AddBackingOffset(ulong backingOffset, ulong offset)
|
||||
{
|
||||
if (backingOffset == ulong.MaxValue)
|
||||
{
|
||||
return backingOffset;
|
||||
}
|
||||
|
||||
return backingOffset + offset;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a region is unmapped.
|
||||
/// </summary>
|
||||
/// <param name="backingOffset">Backing offset to check</param>
|
||||
/// <returns>True if the backing offset is the special "unmapped" value, false otherwise</returns>
|
||||
private static bool IsMapped(ulong backingOffset)
|
||||
{
|
||||
return backingOffset != ulong.MaxValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a protection to the list of protections.
|
||||
/// </summary>
|
||||
/// <param name="address">Address of the protected region</param>
|
||||
/// <param name="size">Size of the protected region in bytes</param>
|
||||
/// <param name="permission">Memory permissions of the region</param>
|
||||
private void AddProtection(ulong address, ulong size, MemoryPermission permission)
|
||||
{
|
||||
ulong endAddress = address + size;
|
||||
var overlaps = Array.Empty<IntervalTreeNode<ulong, MemoryPermission>>();
|
||||
int count;
|
||||
|
||||
lock (_protections)
|
||||
{
|
||||
count = _protections.Get(address, endAddress, ref overlaps);
|
||||
|
||||
if (count == 1 &&
|
||||
overlaps[0].Start <= address &&
|
||||
overlaps[0].End >= endAddress &&
|
||||
overlaps[0].Value == permission)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ulong startAddress = address;
|
||||
|
||||
for (int index = 0; index < count; index++)
|
||||
{
|
||||
var protection = overlaps[index];
|
||||
|
||||
ulong protAddress = protection.Start;
|
||||
ulong protEndAddress = protection.End;
|
||||
MemoryPermission protPermission = protection.Value;
|
||||
|
||||
_protections.Remove(protection);
|
||||
|
||||
if (protection.Value == permission)
|
||||
{
|
||||
if (startAddress > protAddress)
|
||||
{
|
||||
startAddress = protAddress;
|
||||
}
|
||||
|
||||
if (endAddress < protEndAddress)
|
||||
{
|
||||
endAddress = protEndAddress;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (startAddress > protAddress)
|
||||
{
|
||||
_protections.Add(protAddress, startAddress, protPermission);
|
||||
}
|
||||
|
||||
if (endAddress < protEndAddress)
|
||||
{
|
||||
_protections.Add(endAddress, protEndAddress, protPermission);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_protections.Add(startAddress, endAddress, permission);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes protection from the list of protections.
|
||||
/// </summary>
|
||||
/// <param name="address">Address of the protected region</param>
|
||||
/// <param name="size">Size of the protected region in bytes</param>
|
||||
private void RemoveProtection(ulong address, ulong size)
|
||||
{
|
||||
ulong endAddress = address + size;
|
||||
var overlaps = Array.Empty<IntervalTreeNode<ulong, MemoryPermission>>();
|
||||
int count;
|
||||
|
||||
lock (_protections)
|
||||
{
|
||||
count = _protections.Get(address, endAddress, ref overlaps);
|
||||
|
||||
for (int index = 0; index < count; index++)
|
||||
{
|
||||
var protection = overlaps[index];
|
||||
|
||||
ulong protAddress = protection.Start;
|
||||
ulong protEndAddress = protection.End;
|
||||
MemoryPermission protPermission = protection.Value;
|
||||
|
||||
_protections.Remove(protection);
|
||||
|
||||
if (address > protAddress)
|
||||
{
|
||||
_protections.Add(protAddress, address, protPermission);
|
||||
}
|
||||
|
||||
if (endAddress < protEndAddress)
|
||||
{
|
||||
_protections.Add(endAddress, protEndAddress, protPermission);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restores the protection of a given memory region that was remapped, using the protections list.
|
||||
/// </summary>
|
||||
/// <param name="address">Address of the remapped region</param>
|
||||
/// <param name="size">Size of the remapped region in bytes</param>
|
||||
private void RestoreRangeProtection(ulong address, ulong size)
|
||||
{
|
||||
ulong endAddress = address + size;
|
||||
var overlaps = Array.Empty<IntervalTreeNode<ulong, MemoryPermission>>();
|
||||
int count;
|
||||
|
||||
lock (_protections)
|
||||
{
|
||||
count = _protections.Get(address, endAddress, ref overlaps);
|
||||
}
|
||||
|
||||
ulong startAddress = address;
|
||||
|
||||
for (int index = 0; index < count; index++)
|
||||
{
|
||||
var protection = overlaps[index];
|
||||
|
||||
ulong protAddress = protection.Start;
|
||||
ulong protEndAddress = protection.End;
|
||||
|
||||
if (protAddress < address)
|
||||
{
|
||||
protAddress = address;
|
||||
}
|
||||
|
||||
if (protEndAddress > endAddress)
|
||||
{
|
||||
protEndAddress = endAddress;
|
||||
}
|
||||
|
||||
ReprotectViewInternal((IntPtr)protAddress, (IntPtr)(protEndAddress - protAddress), protection.Value, true);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if an access violation handler should retry execution due to a fault caused by partial unmap.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Due to Windows limitations, <see cref="UnmapView"/> might need to unmap more memory than requested.
|
||||
/// The additional memory that was unmapped is later remapped, however this leaves a time gap where the
|
||||
/// memory might be accessed but is unmapped. Users of the API must compensate for that by catching the
|
||||
/// access violation and retrying if it happened between the unmap and remap operation.
|
||||
/// This method can be used to decide if retrying in such cases is necessary or not.
|
||||
/// </remarks>
|
||||
/// <returns>True if execution should be retried, false otherwise</returns>
|
||||
public bool RetryFromAccessViolation()
|
||||
{
|
||||
_partialUnmapLock.AcquireReaderLock(Timeout.Infinite);
|
||||
|
||||
bool retry = _threadLocalPartialUnmapsCount != _partialUnmapsCount;
|
||||
if (retry)
|
||||
{
|
||||
_threadLocalPartialUnmapsCount = _partialUnmapsCount;
|
||||
}
|
||||
|
||||
_partialUnmapLock.ReleaseReaderLock();
|
||||
|
||||
return retry;
|
||||
}
|
||||
}
|
||||
}
|
170
Ryujinx.Memory/WindowsShared/PlaceholderManager4KB.cs
Normal file
170
Ryujinx.Memory/WindowsShared/PlaceholderManager4KB.cs
Normal file
@ -0,0 +1,170 @@
|
||||
using System;
|
||||
using System.Runtime.Versioning;
|
||||
|
||||
namespace Ryujinx.Memory.WindowsShared
|
||||
{
|
||||
/// <summary>
|
||||
/// Windows 4KB memory placeholder manager.
|
||||
/// </summary>
|
||||
[SupportedOSPlatform("windows")]
|
||||
class PlaceholderManager4KB
|
||||
{
|
||||
private const int PageSize = MemoryManagementWindows.PageSize;
|
||||
|
||||
private readonly IntervalTree<ulong, byte> _mappings;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the Windows 4KB memory placeholder manager.
|
||||
/// </summary>
|
||||
public PlaceholderManager4KB()
|
||||
{
|
||||
_mappings = new IntervalTree<ulong, byte>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unmaps the specified range of memory and marks it as mapped internally.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Since this marks the range as mapped, the expectation is that the range will be mapped after calling this method.
|
||||
/// </remarks>
|
||||
/// <param name="location">Memory address to unmap and mark as mapped</param>
|
||||
/// <param name="size">Size of the range in bytes</param>
|
||||
public void UnmapAndMarkRangeAsMapped(IntPtr location, IntPtr size)
|
||||
{
|
||||
ulong startAddress = (ulong)location;
|
||||
ulong unmapSize = (ulong)size;
|
||||
ulong endAddress = startAddress + unmapSize;
|
||||
|
||||
var overlaps = Array.Empty<IntervalTreeNode<ulong, byte>>();
|
||||
int count = 0;
|
||||
|
||||
lock (_mappings)
|
||||
{
|
||||
count = _mappings.Get(startAddress, endAddress, ref overlaps);
|
||||
}
|
||||
|
||||
for (int index = 0; index < count; index++)
|
||||
{
|
||||
var overlap = overlaps[index];
|
||||
|
||||
// Tree operations might modify the node start/end values, so save a copy before we modify the tree.
|
||||
ulong overlapStart = overlap.Start;
|
||||
ulong overlapEnd = overlap.End;
|
||||
ulong overlapValue = overlap.Value;
|
||||
|
||||
_mappings.Remove(overlap);
|
||||
|
||||
ulong unmapStart = Math.Max(overlapStart, startAddress);
|
||||
ulong unmapEnd = Math.Min(overlapEnd, endAddress);
|
||||
|
||||
if (overlapStart < startAddress)
|
||||
{
|
||||
startAddress = overlapStart;
|
||||
}
|
||||
|
||||
if (overlapEnd > endAddress)
|
||||
{
|
||||
endAddress = overlapEnd;
|
||||
}
|
||||
|
||||
ulong currentAddress = unmapStart;
|
||||
while (currentAddress < unmapEnd)
|
||||
{
|
||||
WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)currentAddress, 2);
|
||||
currentAddress += PageSize;
|
||||
}
|
||||
}
|
||||
|
||||
_mappings.Add(startAddress, endAddress, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unmaps views at the specified memory range.
|
||||
/// </summary>
|
||||
/// <param name="location">Address of the range</param>
|
||||
/// <param name="size">Size of the range in bytes</param>
|
||||
public void UnmapView(IntPtr location, IntPtr size)
|
||||
{
|
||||
ulong startAddress = (ulong)location;
|
||||
ulong unmapSize = (ulong)size;
|
||||
ulong endAddress = startAddress + unmapSize;
|
||||
|
||||
var overlaps = Array.Empty<IntervalTreeNode<ulong, byte>>();
|
||||
int count = 0;
|
||||
|
||||
lock (_mappings)
|
||||
{
|
||||
count = _mappings.Get(startAddress, endAddress, ref overlaps);
|
||||
}
|
||||
|
||||
for (int index = 0; index < count; index++)
|
||||
{
|
||||
var overlap = overlaps[index];
|
||||
|
||||
// Tree operations might modify the node start/end values, so save a copy before we modify the tree.
|
||||
ulong overlapStart = overlap.Start;
|
||||
ulong overlapEnd = overlap.End;
|
||||
|
||||
_mappings.Remove(overlap);
|
||||
|
||||
if (overlapStart < startAddress)
|
||||
{
|
||||
_mappings.Add(overlapStart, startAddress, 0);
|
||||
}
|
||||
|
||||
if (overlapEnd > endAddress)
|
||||
{
|
||||
_mappings.Add(endAddress, overlapEnd, 0);
|
||||
}
|
||||
|
||||
ulong unmapStart = Math.Max(overlapStart, startAddress);
|
||||
ulong unmapEnd = Math.Min(overlapEnd, endAddress);
|
||||
|
||||
ulong currentAddress = unmapStart;
|
||||
while (currentAddress < unmapEnd)
|
||||
{
|
||||
WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)currentAddress, 2);
|
||||
currentAddress += PageSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unmaps mapped memory at a given range.
|
||||
/// </summary>
|
||||
/// <param name="location">Address of the range</param>
|
||||
/// <param name="size">Size of the range in bytes</param>
|
||||
public void UnmapRange(IntPtr location, IntPtr size)
|
||||
{
|
||||
ulong startAddress = (ulong)location;
|
||||
ulong unmapSize = (ulong)size;
|
||||
ulong endAddress = startAddress + unmapSize;
|
||||
|
||||
var overlaps = Array.Empty<IntervalTreeNode<ulong, byte>>();
|
||||
int count = 0;
|
||||
|
||||
lock (_mappings)
|
||||
{
|
||||
count = _mappings.Get(startAddress, endAddress, ref overlaps);
|
||||
}
|
||||
|
||||
for (int index = 0; index < count; index++)
|
||||
{
|
||||
var overlap = overlaps[index];
|
||||
|
||||
// Tree operations might modify the node start/end values, so save a copy before we modify the tree.
|
||||
ulong unmapStart = Math.Max(overlap.Start, startAddress);
|
||||
ulong unmapEnd = Math.Min(overlap.End, endAddress);
|
||||
|
||||
_mappings.Remove(overlap);
|
||||
|
||||
ulong currentAddress = unmapStart;
|
||||
while (currentAddress < unmapEnd)
|
||||
{
|
||||
WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)currentAddress, 2);
|
||||
currentAddress += PageSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
93
Ryujinx.Memory/WindowsShared/WindowsApi.cs
Normal file
93
Ryujinx.Memory/WindowsShared/WindowsApi.cs
Normal file
@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ryujinx.Memory.WindowsShared
|
||||
{
|
||||
static class WindowsApi
|
||||
{
|
||||
public static readonly IntPtr InvalidHandleValue = new IntPtr(-1);
|
||||
public static readonly IntPtr CurrentProcessHandle = new IntPtr(-1);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
public static extern IntPtr VirtualAlloc(
|
||||
IntPtr lpAddress,
|
||||
IntPtr dwSize,
|
||||
AllocationType flAllocationType,
|
||||
MemoryProtection flProtect);
|
||||
|
||||
[DllImport("KernelBase.dll", SetLastError = true)]
|
||||
public static extern IntPtr VirtualAlloc2(
|
||||
IntPtr process,
|
||||
IntPtr lpAddress,
|
||||
IntPtr dwSize,
|
||||
AllocationType flAllocationType,
|
||||
MemoryProtection flProtect,
|
||||
IntPtr extendedParameters,
|
||||
ulong parameterCount);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
public static extern bool VirtualProtect(
|
||||
IntPtr lpAddress,
|
||||
IntPtr dwSize,
|
||||
MemoryProtection flNewProtect,
|
||||
out MemoryProtection lpflOldProtect);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
public static extern bool VirtualFree(IntPtr lpAddress, IntPtr dwSize, AllocationType dwFreeType);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
public static extern IntPtr CreateFileMapping(
|
||||
IntPtr hFile,
|
||||
IntPtr lpFileMappingAttributes,
|
||||
FileMapProtection flProtect,
|
||||
uint dwMaximumSizeHigh,
|
||||
uint dwMaximumSizeLow,
|
||||
[MarshalAs(UnmanagedType.LPWStr)] string lpName);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
public static extern bool CloseHandle(IntPtr hObject);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
public static extern IntPtr MapViewOfFile(
|
||||
IntPtr hFileMappingObject,
|
||||
uint dwDesiredAccess,
|
||||
uint dwFileOffsetHigh,
|
||||
uint dwFileOffsetLow,
|
||||
IntPtr dwNumberOfBytesToMap);
|
||||
|
||||
[DllImport("KernelBase.dll", SetLastError = true)]
|
||||
public static extern IntPtr MapViewOfFile3(
|
||||
IntPtr hFileMappingObject,
|
||||
IntPtr process,
|
||||
IntPtr baseAddress,
|
||||
ulong offset,
|
||||
IntPtr dwNumberOfBytesToMap,
|
||||
ulong allocationType,
|
||||
MemoryProtection dwDesiredAccess,
|
||||
IntPtr extendedParameters,
|
||||
ulong parameterCount);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
public static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
|
||||
|
||||
[DllImport("KernelBase.dll", SetLastError = true)]
|
||||
public static extern bool UnmapViewOfFile2(IntPtr process, IntPtr lpBaseAddress, ulong unmapFlags);
|
||||
|
||||
[DllImport("kernel32.dll")]
|
||||
public static extern uint GetLastError();
|
||||
|
||||
public static MemoryProtection GetProtection(MemoryPermission permission)
|
||||
{
|
||||
return permission switch
|
||||
{
|
||||
MemoryPermission.None => MemoryProtection.NoAccess,
|
||||
MemoryPermission.Read => MemoryProtection.ReadOnly,
|
||||
MemoryPermission.ReadAndWrite => MemoryProtection.ReadWrite,
|
||||
MemoryPermission.ReadAndExecute => MemoryProtection.ExecuteRead,
|
||||
MemoryPermission.ReadWriteExecute => MemoryProtection.ExecuteReadWrite,
|
||||
MemoryPermission.Execute => MemoryProtection.Execute,
|
||||
_ => throw new MemoryProtectionException(permission)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
24
Ryujinx.Memory/WindowsShared/WindowsApiException.cs
Normal file
24
Ryujinx.Memory/WindowsShared/WindowsApiException.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
|
||||
namespace Ryujinx.Memory.WindowsShared
|
||||
{
|
||||
class WindowsApiException : Exception
|
||||
{
|
||||
public WindowsApiException()
|
||||
{
|
||||
}
|
||||
|
||||
public WindowsApiException(string functionName) : base(CreateMessage(functionName))
|
||||
{
|
||||
}
|
||||
|
||||
public WindowsApiException(string functionName, Exception inner) : base(CreateMessage(functionName), inner)
|
||||
{
|
||||
}
|
||||
|
||||
private static string CreateMessage(string functionName)
|
||||
{
|
||||
return $"{functionName} returned error code 0x{WindowsApi.GetLastError():X}.";
|
||||
}
|
||||
}
|
||||
}
|
@ -54,9 +54,9 @@ namespace Ryujinx.Tests.Cpu
|
||||
_currAddress = CodeBaseAddress;
|
||||
|
||||
_ram = new MemoryBlock(Size * 2);
|
||||
_memory = new MemoryManager(1ul << 16);
|
||||
_memory = new MemoryManager(_ram, 1ul << 16);
|
||||
_memory.IncrementReferenceCount();
|
||||
_memory.Map(CodeBaseAddress, _ram.GetPointer(0, Size * 2), Size * 2);
|
||||
_memory.Map(CodeBaseAddress, 0, Size * 2);
|
||||
|
||||
_context = CpuContext.CreateExecutionContext();
|
||||
Translator.IsReadyForTranslation.Set();
|
||||
|
@ -49,9 +49,9 @@ namespace Ryujinx.Tests.Cpu
|
||||
_currAddress = CodeBaseAddress;
|
||||
|
||||
_ram = new MemoryBlock(Size * 2);
|
||||
_memory = new MemoryManager(1ul << 16);
|
||||
_memory = new MemoryManager(_ram, 1ul << 16);
|
||||
_memory.IncrementReferenceCount();
|
||||
_memory.Map(CodeBaseAddress, _ram.GetPointer(0, Size * 2), Size * 2);
|
||||
_memory.Map(CodeBaseAddress, 0, Size * 2);
|
||||
|
||||
_context = CpuContext.CreateExecutionContext();
|
||||
_context.IsAarch32 = true;
|
||||
@ -286,6 +286,35 @@ namespace Ryujinx.Tests.Cpu
|
||||
Assert.That(GetContext().Pstate, Is.EqualTo(finalCpsr));
|
||||
}
|
||||
|
||||
public void RunPrecomputedTestCase(PrecomputedMemoryThumbTestCase test)
|
||||
{
|
||||
byte[] testMem = new byte[Size];
|
||||
|
||||
for (ulong i = 0; i < Size; i += 2)
|
||||
{
|
||||
testMem[i + 0] = (byte)((i + DataBaseAddress) >> 0);
|
||||
testMem[i + 1] = (byte)((i + DataBaseAddress) >> 8);
|
||||
}
|
||||
|
||||
SetWorkingMemory(0, testMem);
|
||||
|
||||
RunPrecomputedTestCase(new PrecomputedThumbTestCase(){
|
||||
Instructions = test.Instructions,
|
||||
StartRegs = test.StartRegs,
|
||||
FinalRegs = test.FinalRegs,
|
||||
});
|
||||
|
||||
foreach (var delta in test.MemoryDelta)
|
||||
{
|
||||
testMem[delta.Address - DataBaseAddress + 0] = (byte)(delta.Value >> 0);
|
||||
testMem[delta.Address - DataBaseAddress + 1] = (byte)(delta.Value >> 8);
|
||||
}
|
||||
|
||||
byte[] mem = _memory.GetSpan(DataBaseAddress, (int)Size).ToArray();
|
||||
|
||||
Assert.That(mem, Is.EqualTo(testMem), "testmem");
|
||||
}
|
||||
|
||||
protected void SetWorkingMemory(uint offset, byte[] data)
|
||||
{
|
||||
_memory.Write(DataBaseAddress + offset, data);
|
||||
@ -641,4 +670,4 @@ namespace Ryujinx.Tests.Cpu
|
||||
_context.SetFPstateFlag(FPState.VFlag, (fpscr & (1u << (int)FPState.VFlag)) != 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
522
Ryujinx.Tests/Cpu/CpuTestT32Mem.cs
Normal file
522
Ryujinx.Tests/Cpu/CpuTestT32Mem.cs
Normal file
@ -0,0 +1,522 @@
|
||||
using ARMeilleure.State;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
|
||||
namespace Ryujinx.Tests.Cpu
|
||||
{
|
||||
[Category("T32Mem")]
|
||||
public sealed class CpuTestT32Mem : CpuTest32
|
||||
{
|
||||
[Test]
|
||||
public void TestT32MemImm([ValueSource(nameof(ImmTestCases))] PrecomputedMemoryThumbTestCase test)
|
||||
{
|
||||
RunPrecomputedTestCase(test);
|
||||
}
|
||||
|
||||
public static readonly PrecomputedMemoryThumbTestCase[] ImmTestCases =
|
||||
{
|
||||
// STRB (imm8)
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf80c, 0x1b2f, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x000023bd, 0x000027bb, 0x00002715, 0x000028f5, 0x0000233f, 0x0000213b, 0x00002eea, 0x0000282b, 0x000021e1, 0x0000264c, 0x000029e0, 0x00002ae7, 0x000021ff, 0x000026e3, 0x00000001, 0x800001f0 },
|
||||
FinalRegs = new uint[] { 0x000023bd, 0x000027bb, 0x00002715, 0x000028f5, 0x0000233f, 0x0000213b, 0x00002eea, 0x0000282b, 0x000021e1, 0x0000264c, 0x000029e0, 0x00002ae7, 0x0000222e, 0x000026e3, 0x00000001, 0x800001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x21fe, Value: 0xbbfe) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf80a, 0x2f81, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x0000223c, 0x00002db9, 0x00002900, 0x0000247c, 0x00002b0a, 0x0000266b, 0x000026df, 0x00002447, 0x000024bb, 0x00002687, 0x0000266f, 0x00002a80, 0x000025ff, 0x00002881, 0x00000001, 0xa00001f0 },
|
||||
FinalRegs = new uint[] { 0x0000223c, 0x00002db9, 0x00002900, 0x0000247c, 0x00002b0a, 0x0000266b, 0x000026df, 0x00002447, 0x000024bb, 0x00002687, 0x000026f0, 0x00002a80, 0x000025ff, 0x00002881, 0x00000001, 0xa00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x26f0, Value: 0x2600) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf803, 0x6968, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x000026ed, 0x00002685, 0x00002cd1, 0x00002dac, 0x00002a23, 0x00002626, 0x00002ec9, 0x0000245c, 0x000024ef, 0x00002319, 0x000026ce, 0x0000214d, 0x00002401, 0x000028b4, 0x00000001, 0x300001f0 },
|
||||
FinalRegs = new uint[] { 0x000026ed, 0x00002685, 0x00002cd1, 0x00002d44, 0x00002a23, 0x00002626, 0x00002ec9, 0x0000245c, 0x000024ef, 0x00002319, 0x000026ce, 0x0000214d, 0x00002401, 0x000028b4, 0x00000001, 0x300001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2dac, Value: 0x2dc9) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf804, 0x89ad, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x0000265d, 0x00002b9c, 0x00002360, 0x000029ec, 0x00002413, 0x00002d8e, 0x00002aad, 0x00002d29, 0x00002bca, 0x00002a44, 0x00002980, 0x00002710, 0x000022fa, 0x0000222e, 0x00000001, 0xc00001f0 },
|
||||
FinalRegs = new uint[] { 0x0000265d, 0x00002b9c, 0x00002360, 0x000029ec, 0x00002366, 0x00002d8e, 0x00002aad, 0x00002d29, 0x00002bca, 0x00002a44, 0x00002980, 0x00002710, 0x000022fa, 0x0000222e, 0x00000001, 0xc00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2412, Value: 0xca12) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf80d, 0xa9fe, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x0000298d, 0x00002e6c, 0x00002986, 0x00002ebb, 0x0000213e, 0x00002e39, 0x0000246f, 0x00002b6c, 0x00002ee2, 0x0000259e, 0x0000250a, 0x000029f6, 0x000021e7, 0x00002d9d, 0x00000001, 0x900001f0 },
|
||||
FinalRegs = new uint[] { 0x0000298d, 0x00002e6c, 0x00002986, 0x00002ebb, 0x0000213e, 0x00002e39, 0x0000246f, 0x00002b6c, 0x00002ee2, 0x0000259e, 0x0000250a, 0x000029f6, 0x000021e7, 0x00002c9f, 0x00000001, 0x900001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2d9c, Value: 0x0a9c) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf80d, 0x3c46, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002c6f, 0x000028cc, 0x000025f0, 0x000022cc, 0x00002de3, 0x0000243c, 0x000025fb, 0x00002e88, 0x00002985, 0x000023ee, 0x00002120, 0x00002d50, 0x0000270a, 0x00002bbd, 0x00000001, 0xa00001f0 },
|
||||
FinalRegs = new uint[] { 0x00002c6f, 0x000028cc, 0x000025f0, 0x000022cc, 0x00002de3, 0x0000243c, 0x000025fb, 0x00002e88, 0x00002985, 0x000023ee, 0x00002120, 0x00002d50, 0x0000270a, 0x00002bbd, 0x00000001, 0xa00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2b76, Value: 0xcc76) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf801, 0x6c56, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002d6e, 0x00002530, 0x00002e6d, 0x00002942, 0x00002985, 0x00002d64, 0x00002a73, 0x00002ac6, 0x00002955, 0x00002881, 0x0000221d, 0x00002cb0, 0x0000225f, 0x00002534, 0x00000001, 0x100001f0 },
|
||||
FinalRegs = new uint[] { 0x00002d6e, 0x00002530, 0x00002e6d, 0x00002942, 0x00002985, 0x00002d64, 0x00002a73, 0x00002ac6, 0x00002955, 0x00002881, 0x0000221d, 0x00002cb0, 0x0000225f, 0x00002534, 0x00000001, 0x100001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x24da, Value: 0x2473) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf809, 0xcc76, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002d50, 0x000025f2, 0x0000250a, 0x0000214c, 0x000023d1, 0x00002115, 0x00002c27, 0x00002540, 0x0000222b, 0x00002d03, 0x00002679, 0x00002b52, 0x00002eee, 0x00002b2a, 0x00000001, 0xd00001f0 },
|
||||
FinalRegs = new uint[] { 0x00002d50, 0x000025f2, 0x0000250a, 0x0000214c, 0x000023d1, 0x00002115, 0x00002c27, 0x00002540, 0x0000222b, 0x00002d03, 0x00002679, 0x00002b52, 0x00002eee, 0x00002b2a, 0x00000001, 0xd00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2c8c, Value: 0xee8c) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf808, 0x1c8d, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002844, 0x00002b78, 0x000028b0, 0x000026ff, 0x0000280b, 0x00002e0b, 0x00002de4, 0x00002b53, 0x00002ecd, 0x000021b5, 0x000026bc, 0x00002e9d, 0x00002d33, 0x000027f0, 0x00000001, 0x800001f0 },
|
||||
FinalRegs = new uint[] { 0x00002844, 0x00002b78, 0x000028b0, 0x000026ff, 0x0000280b, 0x00002e0b, 0x00002de4, 0x00002b53, 0x00002ecd, 0x000021b5, 0x000026bc, 0x00002e9d, 0x00002d33, 0x000027f0, 0x00000001, 0x800001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2e40, Value: 0x2e78) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf80b, 0xbc26, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002244, 0x000025ad, 0x00002434, 0x00002b06, 0x00002ebd, 0x0000292b, 0x00002431, 0x00002e12, 0x0000289b, 0x0000265a, 0x00002747, 0x00002bac, 0x00002dae, 0x00002582, 0x00000001, 0xf00001f0 },
|
||||
FinalRegs = new uint[] { 0x00002244, 0x000025ad, 0x00002434, 0x00002b06, 0x00002ebd, 0x0000292b, 0x00002431, 0x00002e12, 0x0000289b, 0x0000265a, 0x00002747, 0x00002bac, 0x00002dae, 0x00002582, 0x00000001, 0xf00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2b86, Value: 0x2bac) },
|
||||
},
|
||||
// STRB (imm12)
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf887, 0x67c2, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x700001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x700001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x27c2, Value: 0x2700) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf883, 0x9fda, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xc00001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xc00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2fda, Value: 0x2f00) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf889, 0xd200, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x400001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x400001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf88c, 0x1c5b, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x500001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x500001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2c5a, Value: 0x005a) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf887, 0x9fe2, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xe00001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xe00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2fe2, Value: 0x2f00) },
|
||||
},
|
||||
// STRH (imm8)
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf826, 0x0b0a, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x000025a2, 0x000024d5, 0x00002ca1, 0x0000238a, 0x0000279c, 0x0000244c, 0x00002620, 0x00002c0e, 0x0000233e, 0x0000285f, 0x000021ab, 0x00002bd0, 0x0000281f, 0x00002be7, 0x00000001, 0x600001f0 },
|
||||
FinalRegs = new uint[] { 0x000025a2, 0x000024d5, 0x00002ca1, 0x0000238a, 0x0000279c, 0x0000244c, 0x0000262a, 0x00002c0e, 0x0000233e, 0x0000285f, 0x000021ab, 0x00002bd0, 0x0000281f, 0x00002be7, 0x00000001, 0x600001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2620, Value: 0x25a2) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf827, 0xcf61, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002555, 0x0000238f, 0x00002829, 0x000028c8, 0x00002399, 0x00002aab, 0x00002d6f, 0x000029eb, 0x000029e0, 0x00002d33, 0x0000292a, 0x00002b33, 0x00002e29, 0x00002ca4, 0x00000001, 0x100001f0 },
|
||||
FinalRegs = new uint[] { 0x00002555, 0x0000238f, 0x00002829, 0x000028c8, 0x00002399, 0x00002aab, 0x00002d6f, 0x00002a4c, 0x000029e0, 0x00002d33, 0x0000292a, 0x00002b33, 0x00002e29, 0x00002ca4, 0x00000001, 0x100001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2a4c, Value: 0x2e29) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf821, 0x9b00, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x000027ba, 0x00002514, 0x00002b07, 0x00002daf, 0x00002790, 0x0000274b, 0x00002379, 0x00002a98, 0x000024c8, 0x00002398, 0x000021ba, 0x00002959, 0x00002821, 0x00002d09, 0x00000001, 0x500001f0 },
|
||||
FinalRegs = new uint[] { 0x000027ba, 0x00002514, 0x00002b07, 0x00002daf, 0x00002790, 0x0000274b, 0x00002379, 0x00002a98, 0x000024c8, 0x00002398, 0x000021ba, 0x00002959, 0x00002821, 0x00002d09, 0x00000001, 0x500001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2514, Value: 0x2398) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf82c, 0xa927, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x0000226a, 0x00002792, 0x00002870, 0x00002918, 0x00002757, 0x00002679, 0x00002546, 0x000027f5, 0x00002edc, 0x00002cd3, 0x0000274a, 0x00002562, 0x000029a1, 0x00002976, 0x00000001, 0x100001f0 },
|
||||
FinalRegs = new uint[] { 0x0000226a, 0x00002792, 0x00002870, 0x00002918, 0x00002757, 0x00002679, 0x00002546, 0x000027f5, 0x00002edc, 0x00002cd3, 0x0000274a, 0x00002562, 0x0000297a, 0x00002976, 0x00000001, 0x100001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x29a0, Value: 0x4aa0), (Address: 0x29a2, Value: 0x2927) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf824, 0xcfe4, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x0000238b, 0x00002d22, 0x00002476, 0x000028ae, 0x00002442, 0x0000212b, 0x000026de, 0x00002a1a, 0x00002a02, 0x00002e47, 0x00002b2d, 0x00002427, 0x00002d1c, 0x000026d4, 0x00000001, 0xd00001f0 },
|
||||
FinalRegs = new uint[] { 0x0000238b, 0x00002d22, 0x00002476, 0x000028ae, 0x00002526, 0x0000212b, 0x000026de, 0x00002a1a, 0x00002a02, 0x00002e47, 0x00002b2d, 0x00002427, 0x00002d1c, 0x000026d4, 0x00000001, 0xd00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2526, Value: 0x2d1c) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf820, 0x1c3d, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002227, 0x00002b29, 0x0000232a, 0x0000214e, 0x000029ef, 0x00002522, 0x000029d3, 0x0000286c, 0x000029b2, 0x00002147, 0x00002c65, 0x00002891, 0x000029c2, 0x000028a5, 0x00000001, 0x800001f0 },
|
||||
FinalRegs = new uint[] { 0x00002227, 0x00002b29, 0x0000232a, 0x0000214e, 0x000029ef, 0x00002522, 0x000029d3, 0x0000286c, 0x000029b2, 0x00002147, 0x00002c65, 0x00002891, 0x000029c2, 0x000028a5, 0x00000001, 0x800001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x21ea, Value: 0x2b29) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf826, 0x1cdf, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002232, 0x000029a1, 0x00002938, 0x00002ae7, 0x000029a4, 0x00002366, 0x0000273a, 0x000023f6, 0x00002601, 0x00002919, 0x000028e3, 0x00002907, 0x000023c1, 0x00002138, 0x00000001, 0x100001f0 },
|
||||
FinalRegs = new uint[] { 0x00002232, 0x000029a1, 0x00002938, 0x00002ae7, 0x000029a4, 0x00002366, 0x0000273a, 0x000023f6, 0x00002601, 0x00002919, 0x000028e3, 0x00002907, 0x000023c1, 0x00002138, 0x00000001, 0x100001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x265a, Value: 0xa15a), (Address: 0x265c, Value: 0x2629) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf82b, 0x3c66, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002974, 0x00002372, 0x0000276c, 0x000021df, 0x00002272, 0x00002928, 0x00002c50, 0x0000290e, 0x00002319, 0x000021d1, 0x00002a82, 0x000027ff, 0x00002730, 0x000027b2, 0x00000001, 0x700001f0 },
|
||||
FinalRegs = new uint[] { 0x00002974, 0x00002372, 0x0000276c, 0x000021df, 0x00002272, 0x00002928, 0x00002c50, 0x0000290e, 0x00002319, 0x000021d1, 0x00002a82, 0x000027ff, 0x00002730, 0x000027b2, 0x00000001, 0x700001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2798, Value: 0xdf98), (Address: 0x279a, Value: 0x2721) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf822, 0x3c06, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x000021b8, 0x00002357, 0x00002b00, 0x00002207, 0x00002648, 0x0000219c, 0x000021d2, 0x000023b0, 0x00002368, 0x00002a41, 0x000026ac, 0x00002a86, 0x00002879, 0x00002c1d, 0x00000001, 0x700001f0 },
|
||||
FinalRegs = new uint[] { 0x000021b8, 0x00002357, 0x00002b00, 0x00002207, 0x00002648, 0x0000219c, 0x000021d2, 0x000023b0, 0x00002368, 0x00002a41, 0x000026ac, 0x00002a86, 0x00002879, 0x00002c1d, 0x00000001, 0x700001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2afa, Value: 0x2207) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf824, 0xac84, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002796, 0x000027c8, 0x0000241b, 0x0000214d, 0x0000220b, 0x00002587, 0x00002130, 0x00002910, 0x00002ac2, 0x00002e74, 0x000028f8, 0x000024bf, 0x0000263a, 0x00002625, 0x00000001, 0x600001f0 },
|
||||
FinalRegs = new uint[] { 0x00002796, 0x000027c8, 0x0000241b, 0x0000214d, 0x0000220b, 0x00002587, 0x00002130, 0x00002910, 0x00002ac2, 0x00002e74, 0x000028f8, 0x000024bf, 0x0000263a, 0x00002625, 0x00000001, 0x600001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2186, Value: 0xf886), (Address: 0x2188, Value: 0x2128) },
|
||||
},
|
||||
// STRH (imm12)
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8a5, 0x59d4, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x000001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x000001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x29d4, Value: 0x2000) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8ac, 0xc533, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xe00001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xe00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2532, Value: 0x0032), (Address: 0x2534, Value: 0x2520) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8a3, 0xb559, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x000001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x000001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2558, Value: 0x0058), (Address: 0x255a, Value: 0x2520) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8a5, 0xdb3a, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xb00001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xb00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2b3a, Value: 0x2000) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8a9, 0x02cc, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xc00001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xc00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x22cc, Value: 0x2000) },
|
||||
},
|
||||
// STR (imm8)
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf846, 0x1fb4, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002b17, 0x0000272f, 0x00002483, 0x0000284c, 0x0000287f, 0x0000238f, 0x0000222d, 0x00002259, 0x0000249d, 0x00002e3f, 0x00002323, 0x00002729, 0x000025c1, 0x00002866, 0x00000001, 0x900001f0 },
|
||||
FinalRegs = new uint[] { 0x00002b17, 0x0000272f, 0x00002483, 0x0000284c, 0x0000287f, 0x0000238f, 0x000022e1, 0x00002259, 0x0000249d, 0x00002e3f, 0x00002323, 0x00002729, 0x000025c1, 0x00002866, 0x00000001, 0x900001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x22e0, Value: 0x2fe0), (Address: 0x22e2, Value: 0x0027), (Address: 0x22e4, Value: 0x2200) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf844, 0x3f11, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x000028e1, 0x00002d48, 0x000027d6, 0x000023ac, 0x000027bb, 0x000026cf, 0x000023c1, 0x00002633, 0x0000214b, 0x00002434, 0x0000239a, 0x000025c6, 0x00002148, 0x00002d1f, 0x00000001, 0x300001f0 },
|
||||
FinalRegs = new uint[] { 0x000028e1, 0x00002d48, 0x000027d6, 0x000023ac, 0x000027cc, 0x000026cf, 0x000023c1, 0x00002633, 0x0000214b, 0x00002434, 0x0000239a, 0x000025c6, 0x00002148, 0x00002d1f, 0x00000001, 0x300001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x27cc, Value: 0x23ac), (Address: 0x27ce, Value: 0x0000) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf847, 0x09c2, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x0000248b, 0x00002396, 0x000023c5, 0x00002be0, 0x0000237d, 0x00002191, 0x00002da0, 0x0000211c, 0x00002d24, 0x000021e6, 0x000024ff, 0x00002268, 0x00002968, 0x0000244d, 0x00000001, 0x800001f0 },
|
||||
FinalRegs = new uint[] { 0x0000248b, 0x00002396, 0x000023c5, 0x00002be0, 0x0000237d, 0x00002191, 0x00002da0, 0x0000205a, 0x00002d24, 0x000021e6, 0x000024ff, 0x00002268, 0x00002968, 0x0000244d, 0x00000001, 0x800001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x211c, Value: 0x248b), (Address: 0x211e, Value: 0x0000) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf84d, 0x7f23, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x000025b0, 0x0000260e, 0x00002343, 0x00002e36, 0x000024c5, 0x000029bc, 0x0000278e, 0x00002b63, 0x00002ce7, 0x000029af, 0x000023bf, 0x00002475, 0x00002197, 0x00002c33, 0x00000001, 0x200001f0 },
|
||||
FinalRegs = new uint[] { 0x000025b0, 0x0000260e, 0x00002343, 0x00002e36, 0x000024c5, 0x000029bc, 0x0000278e, 0x00002b63, 0x00002ce7, 0x000029af, 0x000023bf, 0x00002475, 0x00002197, 0x00002c56, 0x00000001, 0x200001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2c56, Value: 0x2b63), (Address: 0x2c58, Value: 0x0000) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf843, 0x9d24, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002ce4, 0x00002e0e, 0x000026d5, 0x000025fb, 0x00002b78, 0x0000215a, 0x00002af7, 0x0000259c, 0x00002645, 0x000027dc, 0x00002163, 0x000028f5, 0x000029df, 0x0000230b, 0x00000001, 0x500001f0 },
|
||||
FinalRegs = new uint[] { 0x00002ce4, 0x00002e0e, 0x000026d5, 0x000025d7, 0x00002b78, 0x0000215a, 0x00002af7, 0x0000259c, 0x00002645, 0x000027dc, 0x00002163, 0x000028f5, 0x000029df, 0x0000230b, 0x00000001, 0x500001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x25d6, Value: 0xdcd6), (Address: 0x25d8, Value: 0x0027), (Address: 0x25da, Value: 0x2500) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf849, 0xdc1a, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002d98, 0x0000254a, 0x00002540, 0x00002324, 0x0000264e, 0x00002523, 0x0000271f, 0x00002875, 0x000023b3, 0x00002680, 0x00002223, 0x000022bf, 0x000025f4, 0x00002d81, 0x00000001, 0x700001f0 },
|
||||
FinalRegs = new uint[] { 0x00002d98, 0x0000254a, 0x00002540, 0x00002324, 0x0000264e, 0x00002523, 0x0000271f, 0x00002875, 0x000023b3, 0x00002680, 0x00002223, 0x000022bf, 0x000025f4, 0x00002d81, 0x00000001, 0x700001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2666, Value: 0x2d81), (Address: 0x2668, Value: 0x0000) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf849, 0x0cd1, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x0000255a, 0x00002655, 0x00002276, 0x000022e4, 0x00002eef, 0x00002e99, 0x00002b55, 0x00002a40, 0x00002661, 0x00002dbd, 0x00002687, 0x000024e1, 0x000023ea, 0x00002b88, 0x00000001, 0xc00001f0 },
|
||||
FinalRegs = new uint[] { 0x0000255a, 0x00002655, 0x00002276, 0x000022e4, 0x00002eef, 0x00002e99, 0x00002b55, 0x00002a40, 0x00002661, 0x00002dbd, 0x00002687, 0x000024e1, 0x000023ea, 0x00002b88, 0x00000001, 0xc00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2cec, Value: 0x255a), (Address: 0x2cee, Value: 0x0000) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf847, 0x7c96, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x000027f6, 0x0000222a, 0x000024e1, 0x00002a2d, 0x00002ee8, 0x000023f2, 0x000029de, 0x00002a53, 0x000029da, 0x00002d2c, 0x00002d6f, 0x000026b8, 0x00002777, 0x00002e3a, 0x00000001, 0xf00001f0 },
|
||||
FinalRegs = new uint[] { 0x000027f6, 0x0000222a, 0x000024e1, 0x00002a2d, 0x00002ee8, 0x000023f2, 0x000029de, 0x00002a53, 0x000029da, 0x00002d2c, 0x00002d6f, 0x000026b8, 0x00002777, 0x00002e3a, 0x00000001, 0xf00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x29bc, Value: 0x53bc), (Address: 0x29be, Value: 0x002a), (Address: 0x29c0, Value: 0x2900) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf84d, 0x8cbd, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002a58, 0x00002a59, 0x00002dfd, 0x00002ba8, 0x00002929, 0x00002146, 0x00002706, 0x000025f3, 0x000023d7, 0x0000221f, 0x000027ae, 0x00002a6e, 0x00002824, 0x00002357, 0x00000001, 0x600001f0 },
|
||||
FinalRegs = new uint[] { 0x00002a58, 0x00002a59, 0x00002dfd, 0x00002ba8, 0x00002929, 0x00002146, 0x00002706, 0x000025f3, 0x000023d7, 0x0000221f, 0x000027ae, 0x00002a6e, 0x00002824, 0x00002357, 0x00000001, 0x600001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x229a, Value: 0x23d7), (Address: 0x229c, Value: 0x0000) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf846, 0xacaf, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x0000284f, 0x00002def, 0x0000292f, 0x000021e8, 0x0000274e, 0x00002518, 0x00002538, 0x00002375, 0x00002d28, 0x0000229a, 0x0000255f, 0x00002eca, 0x00002e15, 0x000021aa, 0x00000001, 0x100001f0 },
|
||||
FinalRegs = new uint[] { 0x0000284f, 0x00002def, 0x0000292f, 0x000021e8, 0x0000274e, 0x00002518, 0x00002538, 0x00002375, 0x00002d28, 0x0000229a, 0x0000255f, 0x00002eca, 0x00002e15, 0x000021aa, 0x00000001, 0x100001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2488, Value: 0x5f88), (Address: 0x248a, Value: 0x0025), (Address: 0x248c, Value: 0x2400) },
|
||||
},
|
||||
// STR (imm12)
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8cc, 0x1a6e, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x500001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x500001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2a6e, Value: 0x2000), (Address: 0x2a70, Value: 0x0000) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8c9, 0xcfc1, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xe00001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xe00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x2fc0, Value: 0x00c0), (Address: 0x2fc2, Value: 0x0020), (Address: 0x2fc4, Value: 0x2f00) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8c3, 0xb5dd, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x600001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x600001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x25dc, Value: 0x00dc), (Address: 0x25de, Value: 0x0020), (Address: 0x25e0, Value: 0x2500) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8c0, 0x69e9, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xe00001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xe00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x29e8, Value: 0x00e8), (Address: 0x29ea, Value: 0x0020), (Address: 0x29ec, Value: 0x2900) },
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8cd, 0x028f, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x600001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x600001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] { (Address: 0x228e, Value: 0x008e), (Address: 0x2290, Value: 0x0020), (Address: 0x2292, Value: 0x2200) },
|
||||
},
|
||||
// LDRB (imm8)
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf816, 0x1c48, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002cb8, 0x00002345, 0x00002ebc, 0x00002db8, 0x000021d4, 0x000026e4, 0x00002458, 0x000029e3, 0x000028d2, 0x000027f4, 0x000023d6, 0x00002def, 0x0000285c, 0x00002d06, 0x00000001, 0x600001f0 },
|
||||
FinalRegs = new uint[] { 0x00002cb8, 0x00000010, 0x00002ebc, 0x00002db8, 0x000021d4, 0x000026e4, 0x00002458, 0x000029e3, 0x000028d2, 0x000027f4, 0x000023d6, 0x00002def, 0x0000285c, 0x00002d06, 0x00000001, 0x600001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf815, 0x2d6e, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x000021e4, 0x00002425, 0x00002e42, 0x00002a58, 0x00002708, 0x00002965, 0x00002a1d, 0x00002ed5, 0x00002cc4, 0x000026e1, 0x00002b4b, 0x00002ade, 0x00002824, 0x00002975, 0x00000001, 0x100001f0 },
|
||||
FinalRegs = new uint[] { 0x000021e4, 0x00002425, 0x00000028, 0x00002a58, 0x00002708, 0x000028f7, 0x00002a1d, 0x00002ed5, 0x00002cc4, 0x000026e1, 0x00002b4b, 0x00002ade, 0x00002824, 0x00002975, 0x00000001, 0x100001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf818, 0x0d33, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002492, 0x0000214d, 0x00002827, 0x000021af, 0x0000215e, 0x000028d6, 0x000024ec, 0x00002984, 0x0000297b, 0x000024b5, 0x000024ca, 0x0000298f, 0x00002339, 0x00002b7e, 0x00000001, 0xd00001f0 },
|
||||
FinalRegs = new uint[] { 0x00000048, 0x0000214d, 0x00002827, 0x000021af, 0x0000215e, 0x000028d6, 0x000024ec, 0x00002984, 0x00002948, 0x000024b5, 0x000024ca, 0x0000298f, 0x00002339, 0x00002b7e, 0x00000001, 0xd00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf810, 0xbff3, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002ea6, 0x000024fa, 0x00002346, 0x00002748, 0x0000283f, 0x00002770, 0x000023e3, 0x000021aa, 0x0000214a, 0x00002d58, 0x00002159, 0x000022e7, 0x00002242, 0x00002728, 0x00000001, 0x600001f0 },
|
||||
FinalRegs = new uint[] { 0x00002f99, 0x000024fa, 0x00002346, 0x00002748, 0x0000283f, 0x00002770, 0x000023e3, 0x000021aa, 0x0000214a, 0x00002d58, 0x00002159, 0x0000002f, 0x00002242, 0x00002728, 0x00000001, 0x600001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
// LDRB (imm12)
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf892, 0xcc8f, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x100001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x0000002c, 0x00002000, 0x00000001, 0x100001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf89a, 0x7fdc, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x200001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x000000dc, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x200001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf890, 0x5f9f, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x800001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x0000002f, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x800001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf894, 0xdda1, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x900001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x0000002d, 0x00000001, 0x900001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf890, 0xc281, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x100001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000022, 0x00002000, 0x00000001, 0x100001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
// LDRH (imm8)
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf834, 0x89d8, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002a9e, 0x00002d84, 0x00002e9b, 0x00002e7f, 0x000024a2, 0x00002b7b, 0x00002e3b, 0x0000299a, 0x00002dff, 0x00002a9e, 0x000027b2, 0x00002a90, 0x00002883, 0x0000288d, 0x00000001, 0x500001f0 },
|
||||
FinalRegs = new uint[] { 0x00002a9e, 0x00002d84, 0x00002e9b, 0x00002e7f, 0x000023ca, 0x00002b7b, 0x00002e3b, 0x0000299a, 0x000024a2, 0x00002a9e, 0x000027b2, 0x00002a90, 0x00002883, 0x0000288d, 0x00000001, 0x500001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf833, 0x6be4, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x000028bd, 0x00002b0e, 0x00002bc1, 0x00002a83, 0x00002293, 0x00002c7c, 0x00002bfe, 0x00002eb7, 0x0000299b, 0x000026e6, 0x0000219c, 0x00002d5e, 0x00002cd4, 0x000026cf, 0x00000001, 0xd00001f0 },
|
||||
FinalRegs = new uint[] { 0x000028bd, 0x00002b0e, 0x00002bc1, 0x00002b67, 0x00002293, 0x00002c7c, 0x0000842a, 0x00002eb7, 0x0000299b, 0x000026e6, 0x0000219c, 0x00002d5e, 0x00002cd4, 0x000026cf, 0x00000001, 0xd00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf83d, 0x1bca, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x0000250e, 0x00002776, 0x000029e5, 0x0000276e, 0x00002c6b, 0x00002712, 0x00002a85, 0x00002d56, 0x000024c0, 0x00002d86, 0x0000254a, 0x00002549, 0x00002795, 0x00002e97, 0x00000001, 0x200001f0 },
|
||||
FinalRegs = new uint[] { 0x0000250e, 0x0000982e, 0x000029e5, 0x0000276e, 0x00002c6b, 0x00002712, 0x00002a85, 0x00002d56, 0x000024c0, 0x00002d86, 0x0000254a, 0x00002549, 0x00002795, 0x00002f61, 0x00000001, 0x200001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
// LDRH (imm12)
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8b7, 0x92fc, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xa00001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x000022fc, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xa00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8ba, 0xadd9, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xa00001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x0000da2d, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xa00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8bb, 0x0bb0, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xd00001f0 },
|
||||
FinalRegs = new uint[] { 0x00002bb0, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0xd00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8b8, 0xc3f8, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x600001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x000023f8, 0x00002000, 0x00000001, 0x600001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
// LDR (imm8)
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf85b, 0x3fd1, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002a19, 0x00002e5b, 0x0000231b, 0x000021fa, 0x00002e95, 0x00002bd5, 0x00002e9c, 0x00002dfa, 0x000021d8, 0x00002ce1, 0x00002318, 0x00002735, 0x0000247d, 0x00002436, 0x00000001, 0xf00001f0 },
|
||||
FinalRegs = new uint[] { 0x00002a19, 0x00002e5b, 0x0000231b, 0x28082806, 0x00002e95, 0x00002bd5, 0x00002e9c, 0x00002dfa, 0x000021d8, 0x00002ce1, 0x00002318, 0x00002806, 0x0000247d, 0x00002436, 0x00000001, 0xf00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf854, 0xab9e, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x0000214f, 0x00002578, 0x00002a98, 0x000021b0, 0x00002ebb, 0x0000284a, 0x00002319, 0x00002581, 0x00002179, 0x00002594, 0x00002373, 0x000028f4, 0x00002ec5, 0x00002e0a, 0x00000001, 0xb00001f0 },
|
||||
FinalRegs = new uint[] { 0x0000214f, 0x00002578, 0x00002a98, 0x000021b0, 0x00002f59, 0x0000284a, 0x00002319, 0x00002581, 0x00002179, 0x00002594, 0xbe2ebc2e, 0x000028f4, 0x00002ec5, 0x00002e0a, 0x00000001, 0xb00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf852, 0x6d2d, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002e27, 0x00002676, 0x00002bde, 0x000022d9, 0x00002362, 0x00002d4b, 0x00002dab, 0x000022b6, 0x0000229c, 0x00002507, 0x00002848, 0x0000225f, 0x00002ac2, 0x000023c3, 0x00000001, 0xf00001f0 },
|
||||
FinalRegs = new uint[] { 0x00002e27, 0x00002676, 0x00002bb1, 0x000022d9, 0x00002362, 0x00002d4b, 0xb42bb22b, 0x000022b6, 0x0000229c, 0x00002507, 0x00002848, 0x0000225f, 0x00002ac2, 0x000023c3, 0x00000001, 0xf00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf850, 0x8da5, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002559, 0x0000285e, 0x000021de, 0x00002223, 0x000023ff, 0x00002e05, 0x00002bf3, 0x000024a5, 0x00002124, 0x00002768, 0x00002a14, 0x0000219e, 0x00002739, 0x00002e3c, 0x00000001, 0xd00001f0 },
|
||||
FinalRegs = new uint[] { 0x000024b4, 0x0000285e, 0x000021de, 0x00002223, 0x000023ff, 0x00002e05, 0x00002bf3, 0x000024a5, 0x24b624b4, 0x00002768, 0x00002a14, 0x0000219e, 0x00002739, 0x00002e3c, 0x00000001, 0xd00001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf857, 0x19f6, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x000027f5, 0x0000285e, 0x000025f6, 0x00002e22, 0x00002224, 0x00002870, 0x00002ecc, 0x000024cf, 0x00002711, 0x0000241b, 0x00002ddf, 0x00002545, 0x000028ca, 0x000023c5, 0x00000001, 0x400001f0 },
|
||||
FinalRegs = new uint[] { 0x000027f5, 0xd224d024, 0x000025f6, 0x00002e22, 0x00002224, 0x00002870, 0x00002ecc, 0x000023d9, 0x00002711, 0x0000241b, 0x00002ddf, 0x00002545, 0x000028ca, 0x000023c5, 0x00000001, 0x400001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
// LDR (imm12)
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8d1, 0xc65e, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x000001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x2660265e, 0x00002000, 0x00000001, 0x000001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8db, 0xd09b, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x800001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x9e209c20, 0x00000001, 0x800001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8d2, 0x6fde, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x900001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x2fe02fde, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x900001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
new PrecomputedMemoryThumbTestCase()
|
||||
{
|
||||
Instructions = new ushort[] { 0xf8dc, 0x3de5, 0x4770, 0xe7fe },
|
||||
StartRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x500001f0 },
|
||||
FinalRegs = new uint[] { 0x00002000, 0x00002000, 0x00002000, 0xe82de62d, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00002000, 0x00000001, 0x500001f0 },
|
||||
MemoryDelta = new (ulong Address, ushort Value)[] {},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
12
Ryujinx.Tests/Cpu/PrecomputedMemoryThumbTestCase.cs
Normal file
12
Ryujinx.Tests/Cpu/PrecomputedMemoryThumbTestCase.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace Ryujinx.Tests.Cpu
|
||||
{
|
||||
public struct PrecomputedMemoryThumbTestCase
|
||||
{
|
||||
public ushort[] Instructions;
|
||||
public uint[] StartRegs;
|
||||
public uint[] FinalRegs;
|
||||
public (ulong Address, ushort Value)[] MemoryDelta;
|
||||
};
|
||||
}
|
@ -32,8 +32,20 @@ namespace Ryujinx
|
||||
[DllImport("libX11")]
|
||||
private extern static int XInitThreads();
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true)]
|
||||
public static extern int MessageBoxA(IntPtr hWnd, string text, string caption, uint type);
|
||||
|
||||
private const uint MB_ICONWARNING = 0x30;
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
{
|
||||
Version = ReleaseInformations.GetVersion();
|
||||
|
||||
if (OperatingSystem.IsWindows() && !OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17134))
|
||||
{
|
||||
MessageBoxA(IntPtr.Zero, "You are running an outdated version of Windows.\n\nStarting on June 1st 2022, Ryujinx will only support Windows 10 1803 and newer.\n", $"Ryujinx {Version}", MB_ICONWARNING);
|
||||
}
|
||||
|
||||
// Parse Arguments.
|
||||
string launchPathArg = null;
|
||||
string baseDirPathArg = null;
|
||||
@ -82,8 +94,6 @@ namespace Ryujinx
|
||||
// Delete backup files after updating.
|
||||
Task.Run(Updater.CleanupUpdate);
|
||||
|
||||
Version = ReleaseInformations.GetVersion();
|
||||
|
||||
Console.Title = $"Ryujinx Console {Version}";
|
||||
|
||||
// NOTE: GTK3 doesn't init X11 in a multi threaded way.
|
||||
|
Reference in New Issue
Block a user