Merge branch 'master' into memorycacheapplist
This commit is contained in:
commit
89930ba959
53 changed files with 1246 additions and 1263 deletions
17
.github/workflows/release.yml
vendored
17
.github/workflows/release.yml
vendored
|
@ -104,37 +104,30 @@ jobs:
|
||||||
if: matrix.platform.os == 'windows-latest'
|
if: matrix.platform.os == 'windows-latest'
|
||||||
run: |
|
run: |
|
||||||
pushd publish_ava
|
pushd publish_ava
|
||||||
|
cp publish/Ryujinx.exe publish/Ryujinx.Ava.exe
|
||||||
7z a ../release_output/ryujinx-${{ steps.version_info.outputs.build_version }}-${{ matrix.platform.zip_os_name }}.zip publish
|
7z a ../release_output/ryujinx-${{ steps.version_info.outputs.build_version }}-${{ matrix.platform.zip_os_name }}.zip publish
|
||||||
|
7z a ../release_output/test-ava-ryujinx-${{ steps.version_info.outputs.build_version }}-${{ matrix.platform.zip_os_name }}.zip publish
|
||||||
popd
|
popd
|
||||||
|
|
||||||
pushd publish_sdl2_headless
|
pushd publish_sdl2_headless
|
||||||
7z a ../release_output/sdl2-ryujinx-headless-${{ steps.version_info.outputs.build_version }}-${{ matrix.platform.zip_os_name }}.zip publish
|
7z a ../release_output/sdl2-ryujinx-headless-${{ steps.version_info.outputs.build_version }}-${{ matrix.platform.zip_os_name }}.zip publish
|
||||||
popd
|
popd
|
||||||
|
|
||||||
pushd publish_ava
|
|
||||||
mv publish/Ryujinx.exe publish/Ryujinx.Ava.exe
|
|
||||||
7z a ../release_output/test-ava-ryujinx-${{ steps.version_info.outputs.build_version }}-${{ matrix.platform.zip_os_name }}.zip publish
|
|
||||||
popd
|
|
||||||
shell: bash
|
shell: bash
|
||||||
|
|
||||||
- name: Packing Linux builds
|
- name: Packing Linux builds
|
||||||
if: matrix.platform.os == 'ubuntu-latest'
|
if: matrix.platform.os == 'ubuntu-latest'
|
||||||
run: |
|
run: |
|
||||||
pushd publish_ava
|
pushd publish_ava
|
||||||
chmod +x publish/Ryujinx.sh publish/Ryujinx
|
cp publish/Ryujinx publish/Ryujinx.Ava
|
||||||
|
chmod +x publish/Ryujinx.sh publish/Ryujinx.Ava publish/Ryujinx
|
||||||
tar -czvf ../release_output/ryujinx-${{ steps.version_info.outputs.build_version }}-${{ matrix.platform.zip_os_name }}.tar.gz publish
|
tar -czvf ../release_output/ryujinx-${{ steps.version_info.outputs.build_version }}-${{ matrix.platform.zip_os_name }}.tar.gz publish
|
||||||
|
tar -czvf ../release_output/test-ava-ryujinx-${{ steps.version_info.outputs.build_version }}-${{ matrix.platform.zip_os_name }}.tar.gz publish
|
||||||
popd
|
popd
|
||||||
|
|
||||||
pushd publish_sdl2_headless
|
pushd publish_sdl2_headless
|
||||||
chmod +x publish/Ryujinx.sh publish/Ryujinx.Headless.SDL2
|
chmod +x publish/Ryujinx.sh publish/Ryujinx.Headless.SDL2
|
||||||
tar -czvf ../release_output/sdl2-ryujinx-headless-${{ steps.version_info.outputs.build_version }}-${{ matrix.platform.zip_os_name }}.tar.gz publish
|
tar -czvf ../release_output/sdl2-ryujinx-headless-${{ steps.version_info.outputs.build_version }}-${{ matrix.platform.zip_os_name }}.tar.gz publish
|
||||||
popd
|
popd
|
||||||
|
|
||||||
pushd publish_ava
|
|
||||||
mv publish/Ryujinx publish/Ryujinx.Ava
|
|
||||||
chmod +x publish/Ryujinx.sh publish/Ryujinx.Ava
|
|
||||||
tar -czvf ../release_output/test-ava-ryujinx-${{ steps.version_info.outputs.build_version }}-${{ matrix.platform.zip_os_name }}.tar.gz publish
|
|
||||||
popd
|
|
||||||
shell: bash
|
shell: bash
|
||||||
|
|
||||||
- name: Pushing new release
|
- name: Pushing new release
|
||||||
|
|
|
@ -3,13 +3,13 @@
|
||||||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageVersion Include="Avalonia" Version="11.0.7" />
|
<PackageVersion Include="Avalonia" Version="11.0.10" />
|
||||||
<PackageVersion Include="Avalonia.Controls.DataGrid" Version="11.0.7" />
|
<PackageVersion Include="Avalonia.Controls.DataGrid" Version="11.0.10" />
|
||||||
<PackageVersion Include="Avalonia.Desktop" Version="11.0.7" />
|
<PackageVersion Include="Avalonia.Desktop" Version="11.0.10" />
|
||||||
<PackageVersion Include="Avalonia.Diagnostics" Version="11.0.7" />
|
<PackageVersion Include="Avalonia.Diagnostics" Version="11.0.10" />
|
||||||
<PackageVersion Include="Avalonia.Markup.Xaml.Loader" Version="11.0.7" />
|
<PackageVersion Include="Avalonia.Markup.Xaml.Loader" Version="11.0.10" />
|
||||||
<PackageVersion Include="Avalonia.Svg" Version="11.0.0.13" />
|
<PackageVersion Include="Avalonia.Svg" Version="11.0.0.16" />
|
||||||
<PackageVersion Include="Avalonia.Svg.Skia" Version="11.0.0.13" />
|
<PackageVersion Include="Avalonia.Svg.Skia" Version="11.0.0.16" />
|
||||||
<PackageVersion Include="CommandLineParser" Version="2.9.1" />
|
<PackageVersion Include="CommandLineParser" Version="2.9.1" />
|
||||||
<PackageVersion Include="Concentus" Version="1.1.7" />
|
<PackageVersion Include="Concentus" Version="1.1.7" />
|
||||||
<PackageVersion Include="DiscordRichPresence" Version="1.2.1.24" />
|
<PackageVersion Include="DiscordRichPresence" Version="1.2.1.24" />
|
||||||
|
@ -19,9 +19,9 @@
|
||||||
<PackageVersion Include="GtkSharp.Dependencies.osx" Version="0.0.5" />
|
<PackageVersion Include="GtkSharp.Dependencies.osx" Version="0.0.5" />
|
||||||
<PackageVersion Include="LibHac" Version="0.19.0" />
|
<PackageVersion Include="LibHac" Version="0.19.0" />
|
||||||
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
|
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
|
||||||
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
|
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" />
|
||||||
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
|
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
|
||||||
<PackageVersion Include="Microsoft.IdentityModel.JsonWebTokens" Version="7.3.0" />
|
<PackageVersion Include="Microsoft.IdentityModel.JsonWebTokens" Version="7.4.0" />
|
||||||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
|
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
|
||||||
<PackageVersion Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" />
|
<PackageVersion Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" />
|
||||||
<PackageVersion Include="MsgPack.Cli" Version="1.0.1" />
|
<PackageVersion Include="MsgPack.Cli" Version="1.0.1" />
|
||||||
|
@ -43,10 +43,9 @@
|
||||||
<PackageVersion Include="Silk.NET.Vulkan" Version="2.16.0" />
|
<PackageVersion Include="Silk.NET.Vulkan" Version="2.16.0" />
|
||||||
<PackageVersion Include="Silk.NET.Vulkan.Extensions.EXT" Version="2.16.0" />
|
<PackageVersion Include="Silk.NET.Vulkan.Extensions.EXT" Version="2.16.0" />
|
||||||
<PackageVersion Include="Silk.NET.Vulkan.Extensions.KHR" Version="2.16.0" />
|
<PackageVersion Include="Silk.NET.Vulkan.Extensions.KHR" Version="2.16.0" />
|
||||||
<PackageVersion Include="SixLabors.ImageSharp" Version="1.0.4" />
|
<PackageVersion Include="SixLabors.ImageSharp" Version="2.1.7" />
|
||||||
<PackageVersion Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta11" />
|
<PackageVersion Include="SixLabors.ImageSharp.Drawing" Version="1.0.0" />
|
||||||
<PackageVersion Include="SPB" Version="0.0.4-build32" />
|
<PackageVersion Include="SPB" Version="0.0.4-build32" />
|
||||||
<PackageVersion Include="System.Drawing.Common" Version="8.0.2" />
|
|
||||||
<PackageVersion Include="System.IO.Hashing" Version="8.0.0" />
|
<PackageVersion Include="System.IO.Hashing" Version="8.0.0" />
|
||||||
<PackageVersion Include="System.Management" Version="8.0.0" />
|
<PackageVersion Include="System.Management" Version="8.0.0" />
|
||||||
<PackageVersion Include="UnicornEngine.Unicorn" Version="2.0.2-rc1-fb78016" />
|
<PackageVersion Include="UnicornEngine.Unicorn" Version="2.0.2-rc1-fb78016" />
|
||||||
|
|
|
@ -91,54 +91,54 @@ namespace ARMeilleure.Instructions
|
||||||
#region "Read"
|
#region "Read"
|
||||||
public static byte ReadByte(ulong address)
|
public static byte ReadByte(ulong address)
|
||||||
{
|
{
|
||||||
return GetMemoryManager().ReadTracked<byte>(address);
|
return GetMemoryManager().ReadGuest<byte>(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ushort ReadUInt16(ulong address)
|
public static ushort ReadUInt16(ulong address)
|
||||||
{
|
{
|
||||||
return GetMemoryManager().ReadTracked<ushort>(address);
|
return GetMemoryManager().ReadGuest<ushort>(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static uint ReadUInt32(ulong address)
|
public static uint ReadUInt32(ulong address)
|
||||||
{
|
{
|
||||||
return GetMemoryManager().ReadTracked<uint>(address);
|
return GetMemoryManager().ReadGuest<uint>(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ulong ReadUInt64(ulong address)
|
public static ulong ReadUInt64(ulong address)
|
||||||
{
|
{
|
||||||
return GetMemoryManager().ReadTracked<ulong>(address);
|
return GetMemoryManager().ReadGuest<ulong>(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static V128 ReadVector128(ulong address)
|
public static V128 ReadVector128(ulong address)
|
||||||
{
|
{
|
||||||
return GetMemoryManager().ReadTracked<V128>(address);
|
return GetMemoryManager().ReadGuest<V128>(address);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region "Write"
|
#region "Write"
|
||||||
public static void WriteByte(ulong address, byte value)
|
public static void WriteByte(ulong address, byte value)
|
||||||
{
|
{
|
||||||
GetMemoryManager().Write(address, value);
|
GetMemoryManager().WriteGuest(address, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void WriteUInt16(ulong address, ushort value)
|
public static void WriteUInt16(ulong address, ushort value)
|
||||||
{
|
{
|
||||||
GetMemoryManager().Write(address, value);
|
GetMemoryManager().WriteGuest(address, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void WriteUInt32(ulong address, uint value)
|
public static void WriteUInt32(ulong address, uint value)
|
||||||
{
|
{
|
||||||
GetMemoryManager().Write(address, value);
|
GetMemoryManager().WriteGuest(address, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void WriteUInt64(ulong address, ulong value)
|
public static void WriteUInt64(ulong address, ulong value)
|
||||||
{
|
{
|
||||||
GetMemoryManager().Write(address, value);
|
GetMemoryManager().WriteGuest(address, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void WriteVector128(ulong address, V128 value)
|
public static void WriteVector128(ulong address, V128 value)
|
||||||
{
|
{
|
||||||
GetMemoryManager().Write(address, value);
|
GetMemoryManager().WriteGuest(address, value);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,17 @@ namespace ARMeilleure.Memory
|
||||||
/// <returns>The data</returns>
|
/// <returns>The data</returns>
|
||||||
T ReadTracked<T>(ulong va) where T : unmanaged;
|
T ReadTracked<T>(ulong va) where T : unmanaged;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reads data from CPU mapped memory, from guest code. (with read tracking)
|
||||||
|
/// </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>
|
||||||
|
T ReadGuest<T>(ulong va) where T : unmanaged
|
||||||
|
{
|
||||||
|
return ReadTracked<T>(va);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes data to CPU mapped memory.
|
/// Writes data to CPU mapped memory.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -36,6 +47,17 @@ namespace ARMeilleure.Memory
|
||||||
/// <param name="value">Data to be written</param>
|
/// <param name="value">Data to be written</param>
|
||||||
void Write<T>(ulong va, T value) where T : unmanaged;
|
void Write<T>(ulong va, T value) where T : unmanaged;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Writes data to CPU mapped memory, from guest code.
|
||||||
|
/// </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>
|
||||||
|
void WriteGuest<T>(ulong va, T value) where T : unmanaged
|
||||||
|
{
|
||||||
|
Write(va, value);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a read-only span of data from CPU mapped memory.
|
/// Gets a read-only span of data from CPU mapped memory.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace Ryujinx.Common.Utilities
|
namespace Ryujinx.Common.Utilities
|
||||||
{
|
{
|
||||||
|
@ -44,5 +46,11 @@ namespace Ryujinx.Common.Utilities
|
||||||
CopyDirectory(sourceDir, destinationDir, true);
|
CopyDirectory(sourceDir, destinationDir, true);
|
||||||
Directory.Delete(sourceDir, true);
|
Directory.Delete(sourceDir, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string SanitizeFileName(string fileName)
|
||||||
|
{
|
||||||
|
var reservedChars = new HashSet<char>(Path.GetInvalidFileNameChars());
|
||||||
|
return string.Concat(fileName.Select(c => reservedChars.Contains(c) ? '_' : c));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ using System.Linq;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Runtime.Versioning;
|
using System.Runtime.Versioning;
|
||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace Ryujinx.Cpu.AppleHv
|
namespace Ryujinx.Cpu.AppleHv
|
||||||
{
|
{
|
||||||
|
@ -16,31 +15,10 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
/// Represents a CPU memory manager which maps guest virtual memory directly onto the Hypervisor page table.
|
/// Represents a CPU memory manager which maps guest virtual memory directly onto the Hypervisor page table.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[SupportedOSPlatform("macos")]
|
[SupportedOSPlatform("macos")]
|
||||||
public class HvMemoryManager : MemoryManagerBase, IMemoryManager, IVirtualMemoryManagerTracked, IWritableBlock
|
public class HvMemoryManager : VirtualMemoryManagerRefCountedBase<ulong, ulong>, IMemoryManager, IVirtualMemoryManagerTracked, IWritableBlock
|
||||||
{
|
{
|
||||||
public const int PageBits = 12;
|
|
||||||
public const int PageSize = 1 << PageBits;
|
|
||||||
public const int PageMask = PageSize - 1;
|
|
||||||
|
|
||||||
public const int PageToPteShift = 5; // 32 pages (2 bits each) in one ulong page table entry.
|
|
||||||
public const ulong BlockMappedMask = 0x5555555555555555; // First bit of each table entry set.
|
|
||||||
|
|
||||||
private enum HostMappedPtBits : ulong
|
|
||||||
{
|
|
||||||
Unmapped = 0,
|
|
||||||
Mapped,
|
|
||||||
WriteTracked,
|
|
||||||
ReadWriteTracked,
|
|
||||||
|
|
||||||
MappedReplicated = 0x5555555555555555,
|
|
||||||
WriteTrackedReplicated = 0xaaaaaaaaaaaaaaaa,
|
|
||||||
ReadWriteTrackedReplicated = ulong.MaxValue,
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly InvalidAccessHandler _invalidAccessHandler;
|
private readonly InvalidAccessHandler _invalidAccessHandler;
|
||||||
|
|
||||||
private readonly ulong _addressSpaceSize;
|
|
||||||
|
|
||||||
private readonly HvAddressSpace _addressSpace;
|
private readonly HvAddressSpace _addressSpace;
|
||||||
|
|
||||||
internal HvAddressSpace AddressSpace => _addressSpace;
|
internal HvAddressSpace AddressSpace => _addressSpace;
|
||||||
|
@ -48,7 +26,7 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
private readonly MemoryBlock _backingMemory;
|
private readonly MemoryBlock _backingMemory;
|
||||||
private readonly PageTable<ulong> _pageTable;
|
private readonly PageTable<ulong> _pageTable;
|
||||||
|
|
||||||
private readonly ulong[] _pageBitmap;
|
private readonly ManagedPageFlags _pages;
|
||||||
|
|
||||||
public bool Supports4KBPages => true;
|
public bool Supports4KBPages => true;
|
||||||
|
|
||||||
|
@ -62,6 +40,8 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
|
|
||||||
public event Action<ulong, ulong> UnmapEvent;
|
public event Action<ulong, ulong> UnmapEvent;
|
||||||
|
|
||||||
|
protected override ulong AddressSpaceSize { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of the Hypervisor memory manager.
|
/// Creates a new instance of the Hypervisor memory manager.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -73,7 +53,7 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
_backingMemory = backingMemory;
|
_backingMemory = backingMemory;
|
||||||
_pageTable = new PageTable<ulong>();
|
_pageTable = new PageTable<ulong>();
|
||||||
_invalidAccessHandler = invalidAccessHandler;
|
_invalidAccessHandler = invalidAccessHandler;
|
||||||
_addressSpaceSize = addressSpaceSize;
|
AddressSpaceSize = addressSpaceSize;
|
||||||
|
|
||||||
ulong asSize = PageSize;
|
ulong asSize = PageSize;
|
||||||
int asBits = PageBits;
|
int asBits = PageBits;
|
||||||
|
@ -88,46 +68,10 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
|
|
||||||
AddressSpaceBits = asBits;
|
AddressSpaceBits = asBits;
|
||||||
|
|
||||||
_pageBitmap = new ulong[1 << (AddressSpaceBits - (PageBits + PageToPteShift))];
|
_pages = new ManagedPageFlags(AddressSpaceBits);
|
||||||
Tracking = new MemoryTracking(this, PageSize, invalidAccessHandler);
|
Tracking = new MemoryTracking(this, PageSize, invalidAccessHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks if the virtual address is part of the addressable space.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="va">Virtual address</param>
|
|
||||||
/// <returns>True if the virtual address is part of the addressable space</returns>
|
|
||||||
private bool ValidateAddress(ulong va)
|
|
||||||
{
|
|
||||||
return va < _addressSpaceSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks if the combination of virtual address and size is part of the addressable space.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="va">Virtual address of the range</param>
|
|
||||||
/// <param name="size">Size of the range in bytes</param>
|
|
||||||
/// <returns>True if the combination of virtual address and size is part of the addressable space</returns>
|
|
||||||
private bool ValidateAddressAndSize(ulong va, ulong size)
|
|
||||||
{
|
|
||||||
ulong endVa = va + size;
|
|
||||||
return endVa >= va && endVa >= size && endVa <= _addressSpaceSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Ensures the combination of virtual address and size is part of the addressable space.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="va">Virtual address of the range</param>
|
|
||||||
/// <param name="size">Size of the range in bytes</param>
|
|
||||||
/// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified outside the addressable space</exception>
|
|
||||||
private void AssertValidAddressAndSize(ulong va, ulong size)
|
|
||||||
{
|
|
||||||
if (!ValidateAddressAndSize(va, size))
|
|
||||||
{
|
|
||||||
throw new InvalidMemoryRegionException($"va=0x{va:X16}, size=0x{size:X16}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public void Map(ulong va, ulong pa, ulong size, MemoryMapFlags flags)
|
public void Map(ulong va, ulong pa, ulong size, MemoryMapFlags flags)
|
||||||
{
|
{
|
||||||
|
@ -135,7 +79,7 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
|
|
||||||
PtMap(va, pa, size);
|
PtMap(va, pa, size);
|
||||||
_addressSpace.MapUser(va, pa, size, MemoryPermission.ReadWriteExecute);
|
_addressSpace.MapUser(va, pa, size, MemoryPermission.ReadWriteExecute);
|
||||||
AddMapping(va, size);
|
_pages.AddMapping(va, size);
|
||||||
|
|
||||||
Tracking.Map(va, size);
|
Tracking.Map(va, size);
|
||||||
}
|
}
|
||||||
|
@ -166,7 +110,7 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
UnmapEvent?.Invoke(va, size);
|
UnmapEvent?.Invoke(va, size);
|
||||||
Tracking.Unmap(va, size);
|
Tracking.Unmap(va, size);
|
||||||
|
|
||||||
RemoveMapping(va, size);
|
_pages.RemoveMapping(va, size);
|
||||||
_addressSpace.UnmapUser(va, size);
|
_addressSpace.UnmapUser(va, size);
|
||||||
PtUnmap(va, size);
|
PtUnmap(va, size);
|
||||||
}
|
}
|
||||||
|
@ -209,9 +153,19 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public void Read(ulong va, Span<byte> data)
|
public override void Read(ulong va, Span<byte> data)
|
||||||
{
|
{
|
||||||
ReadImpl(va, data);
|
try
|
||||||
|
{
|
||||||
|
base.Read(va, data);
|
||||||
|
}
|
||||||
|
catch (InvalidMemoryRegionException)
|
||||||
|
{
|
||||||
|
if (_invalidAccessHandler == null || !_invalidAccessHandler(va))
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
@ -340,7 +294,7 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
{
|
{
|
||||||
Span<byte> data = new byte[size];
|
Span<byte> data = new byte[size];
|
||||||
|
|
||||||
ReadImpl(va, data);
|
base.Read(va, data);
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
@ -367,7 +321,7 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
{
|
{
|
||||||
Memory<byte> memory = new byte[size];
|
Memory<byte> memory = new byte[size];
|
||||||
|
|
||||||
ReadImpl(va, memory.Span);
|
base.Read(va, memory.Span);
|
||||||
|
|
||||||
return new WritableRegion(this, va, memory);
|
return new WritableRegion(this, va, memory);
|
||||||
}
|
}
|
||||||
|
@ -390,22 +344,7 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public bool IsMapped(ulong va)
|
public bool IsMapped(ulong va)
|
||||||
{
|
{
|
||||||
return ValidateAddress(va) && IsMappedImpl(va);
|
return ValidateAddress(va) && _pages.IsMapped(va);
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
private bool IsMappedImpl(ulong va)
|
|
||||||
{
|
|
||||||
ulong page = va >> PageBits;
|
|
||||||
|
|
||||||
int bit = (int)((page & 31) << 1);
|
|
||||||
|
|
||||||
int pageIndex = (int)(page >> PageToPteShift);
|
|
||||||
ref ulong pageRef = ref _pageBitmap[pageIndex];
|
|
||||||
|
|
||||||
ulong pte = Volatile.Read(ref pageRef);
|
|
||||||
|
|
||||||
return ((pte >> bit) & 3) != 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
@ -413,58 +352,7 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
{
|
{
|
||||||
AssertValidAddressAndSize(va, size);
|
AssertValidAddressAndSize(va, size);
|
||||||
|
|
||||||
return IsRangeMappedImpl(va, size);
|
return _pages.IsRangeMapped(va, size);
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
private static void GetPageBlockRange(ulong pageStart, ulong pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex)
|
|
||||||
{
|
|
||||||
startMask = ulong.MaxValue << ((int)(pageStart & 31) << 1);
|
|
||||||
endMask = ulong.MaxValue >> (64 - ((int)(pageEnd & 31) << 1));
|
|
||||||
|
|
||||||
pageIndex = (int)(pageStart >> PageToPteShift);
|
|
||||||
pageEndIndex = (int)((pageEnd - 1) >> PageToPteShift);
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool IsRangeMappedImpl(ulong va, ulong size)
|
|
||||||
{
|
|
||||||
int pages = GetPagesCount(va, size, out _);
|
|
||||||
|
|
||||||
if (pages == 1)
|
|
||||||
{
|
|
||||||
return IsMappedImpl(va);
|
|
||||||
}
|
|
||||||
|
|
||||||
ulong pageStart = va >> PageBits;
|
|
||||||
ulong pageEnd = pageStart + (ulong)pages;
|
|
||||||
|
|
||||||
GetPageBlockRange(pageStart, pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex);
|
|
||||||
|
|
||||||
// Check if either bit in each 2 bit page entry is set.
|
|
||||||
// OR the block with itself shifted down by 1, and check the first bit of each entry.
|
|
||||||
|
|
||||||
ulong mask = BlockMappedMask & startMask;
|
|
||||||
|
|
||||||
while (pageIndex <= pageEndIndex)
|
|
||||||
{
|
|
||||||
if (pageIndex == pageEndIndex)
|
|
||||||
{
|
|
||||||
mask &= endMask;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
|
||||||
ulong pte = Volatile.Read(ref pageRef);
|
|
||||||
|
|
||||||
pte |= pte >> 1;
|
|
||||||
if ((pte & mask) != mask)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
mask = BlockMappedMask;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException();
|
private static void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException();
|
||||||
|
@ -576,48 +464,6 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
return regions;
|
return regions;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ReadImpl(ulong va, Span<byte> data)
|
|
||||||
{
|
|
||||||
if (data.Length == 0)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
AssertValidAddressAndSize(va, (ulong)data.Length);
|
|
||||||
|
|
||||||
int offset = 0, size;
|
|
||||||
|
|
||||||
if ((va & PageMask) != 0)
|
|
||||||
{
|
|
||||||
ulong pa = GetPhysicalAddressChecked(va);
|
|
||||||
|
|
||||||
size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
|
|
||||||
|
|
||||||
_backingMemory.GetSpan(pa, size).CopyTo(data[..size]);
|
|
||||||
|
|
||||||
offset += size;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (; offset < data.Length; offset += size)
|
|
||||||
{
|
|
||||||
ulong pa = GetPhysicalAddressChecked(va + (ulong)offset);
|
|
||||||
|
|
||||||
size = Math.Min(data.Length - offset, PageSize);
|
|
||||||
|
|
||||||
_backingMemory.GetSpan(pa, size).CopyTo(data.Slice(offset, size));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (InvalidMemoryRegionException)
|
|
||||||
{
|
|
||||||
if (_invalidAccessHandler == null || !_invalidAccessHandler(va))
|
|
||||||
{
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// This function also validates that the given range is both valid and mapped, and will throw if it is not.
|
/// This function also validates that the given range is both valid and mapped, and will throw if it is not.
|
||||||
|
@ -632,76 +478,7 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Software table, used for managed memory tracking.
|
_pages.SignalMemoryTracking(Tracking, va, size, write, exemptId);
|
||||||
|
|
||||||
int pages = GetPagesCount(va, size, out _);
|
|
||||||
ulong pageStart = va >> PageBits;
|
|
||||||
|
|
||||||
if (pages == 1)
|
|
||||||
{
|
|
||||||
ulong tag = (ulong)(write ? HostMappedPtBits.WriteTracked : HostMappedPtBits.ReadWriteTracked);
|
|
||||||
|
|
||||||
int bit = (int)((pageStart & 31) << 1);
|
|
||||||
|
|
||||||
int pageIndex = (int)(pageStart >> PageToPteShift);
|
|
||||||
ref ulong pageRef = ref _pageBitmap[pageIndex];
|
|
||||||
|
|
||||||
ulong pte = Volatile.Read(ref pageRef);
|
|
||||||
ulong state = ((pte >> bit) & 3);
|
|
||||||
|
|
||||||
if (state >= tag)
|
|
||||||
{
|
|
||||||
Tracking.VirtualMemoryEvent(va, size, write, precise: false, exemptId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else if (state == 0)
|
|
||||||
{
|
|
||||||
ThrowInvalidMemoryRegionException($"Not mapped: va=0x{va:X16}, size=0x{size:X16}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ulong pageEnd = pageStart + (ulong)pages;
|
|
||||||
|
|
||||||
GetPageBlockRange(pageStart, pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex);
|
|
||||||
|
|
||||||
ulong mask = startMask;
|
|
||||||
|
|
||||||
ulong anyTrackingTag = (ulong)HostMappedPtBits.WriteTrackedReplicated;
|
|
||||||
|
|
||||||
while (pageIndex <= pageEndIndex)
|
|
||||||
{
|
|
||||||
if (pageIndex == pageEndIndex)
|
|
||||||
{
|
|
||||||
mask &= endMask;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
|
||||||
|
|
||||||
ulong pte = Volatile.Read(ref pageRef);
|
|
||||||
ulong mappedMask = mask & BlockMappedMask;
|
|
||||||
|
|
||||||
ulong mappedPte = pte | (pte >> 1);
|
|
||||||
if ((mappedPte & mappedMask) != mappedMask)
|
|
||||||
{
|
|
||||||
ThrowInvalidMemoryRegionException($"Not mapped: va=0x{va:X16}, size=0x{size:X16}");
|
|
||||||
}
|
|
||||||
|
|
||||||
pte &= mask;
|
|
||||||
if ((pte & anyTrackingTag) != 0) // Search for any tracking.
|
|
||||||
{
|
|
||||||
// Writes trigger any tracking.
|
|
||||||
// Only trigger tracking from reads if both bits are set on any page.
|
|
||||||
if (write || (pte & (pte >> 1) & BlockMappedMask) != 0)
|
|
||||||
{
|
|
||||||
Tracking.VirtualMemoryEvent(va, size, write, precise: false, exemptId);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mask = ulong.MaxValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -728,103 +505,28 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public void TrackingReprotect(ulong va, ulong size, MemoryPermission protection)
|
public void TrackingReprotect(ulong va, ulong size, MemoryPermission protection, bool guest)
|
||||||
{
|
{
|
||||||
// Protection is inverted on software pages, since the default value is 0.
|
if (guest)
|
||||||
protection = (~protection) & MemoryPermission.ReadAndWrite;
|
|
||||||
|
|
||||||
int pages = GetPagesCount(va, size, out va);
|
|
||||||
ulong pageStart = va >> PageBits;
|
|
||||||
|
|
||||||
if (pages == 1)
|
|
||||||
{
|
{
|
||||||
ulong protTag = protection switch
|
_addressSpace.ReprotectUser(va, size, protection);
|
||||||
{
|
|
||||||
MemoryPermission.None => (ulong)HostMappedPtBits.Mapped,
|
|
||||||
MemoryPermission.Write => (ulong)HostMappedPtBits.WriteTracked,
|
|
||||||
_ => (ulong)HostMappedPtBits.ReadWriteTracked,
|
|
||||||
};
|
|
||||||
|
|
||||||
int bit = (int)((pageStart & 31) << 1);
|
|
||||||
|
|
||||||
ulong tagMask = 3UL << bit;
|
|
||||||
ulong invTagMask = ~tagMask;
|
|
||||||
|
|
||||||
ulong tag = protTag << bit;
|
|
||||||
|
|
||||||
int pageIndex = (int)(pageStart >> PageToPteShift);
|
|
||||||
ref ulong pageRef = ref _pageBitmap[pageIndex];
|
|
||||||
|
|
||||||
ulong pte;
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
pte = Volatile.Read(ref pageRef);
|
|
||||||
}
|
|
||||||
while ((pte & tagMask) != 0 && Interlocked.CompareExchange(ref pageRef, (pte & invTagMask) | tag, pte) != pte);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ulong pageEnd = pageStart + (ulong)pages;
|
_pages.TrackingReprotect(va, size, protection);
|
||||||
|
|
||||||
GetPageBlockRange(pageStart, pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex);
|
|
||||||
|
|
||||||
ulong mask = startMask;
|
|
||||||
|
|
||||||
ulong protTag = protection switch
|
|
||||||
{
|
|
||||||
MemoryPermission.None => (ulong)HostMappedPtBits.MappedReplicated,
|
|
||||||
MemoryPermission.Write => (ulong)HostMappedPtBits.WriteTrackedReplicated,
|
|
||||||
_ => (ulong)HostMappedPtBits.ReadWriteTrackedReplicated,
|
|
||||||
};
|
|
||||||
|
|
||||||
while (pageIndex <= pageEndIndex)
|
|
||||||
{
|
|
||||||
if (pageIndex == pageEndIndex)
|
|
||||||
{
|
|
||||||
mask &= endMask;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
|
||||||
|
|
||||||
ulong pte;
|
|
||||||
ulong mappedMask;
|
|
||||||
|
|
||||||
// Change the protection of all 2 bit entries that are mapped.
|
|
||||||
do
|
|
||||||
{
|
|
||||||
pte = Volatile.Read(ref pageRef);
|
|
||||||
|
|
||||||
mappedMask = pte | (pte >> 1);
|
|
||||||
mappedMask |= (mappedMask & BlockMappedMask) << 1;
|
|
||||||
mappedMask &= mask; // Only update mapped pages within the given range.
|
|
||||||
}
|
|
||||||
while (Interlocked.CompareExchange(ref pageRef, (pte & (~mappedMask)) | (protTag & mappedMask), pte) != pte);
|
|
||||||
|
|
||||||
mask = ulong.MaxValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protection = protection switch
|
|
||||||
{
|
|
||||||
MemoryPermission.None => MemoryPermission.ReadAndWrite,
|
|
||||||
MemoryPermission.Write => MemoryPermission.Read,
|
|
||||||
_ => MemoryPermission.None,
|
|
||||||
};
|
|
||||||
|
|
||||||
_addressSpace.ReprotectUser(va, size, protection);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public RegionHandle BeginTracking(ulong address, ulong size, int id)
|
public RegionHandle BeginTracking(ulong address, ulong size, int id, RegionFlags flags = RegionFlags.None)
|
||||||
{
|
{
|
||||||
return Tracking.BeginTracking(address, size, id);
|
return Tracking.BeginTracking(address, size, id, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public MultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity, int id)
|
public MultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity, int id, RegionFlags flags = RegionFlags.None)
|
||||||
{
|
{
|
||||||
return Tracking.BeginGranularTracking(address, size, handles, granularity, id);
|
return Tracking.BeginGranularTracking(address, size, handles, granularity, id, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
@ -833,86 +535,6 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
return Tracking.BeginSmartGranularTracking(address, size, granularity, id);
|
return Tracking.BeginSmartGranularTracking(address, size, granularity, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds the given address mapping to the page table.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="va">Virtual memory address</param>
|
|
||||||
/// <param name="size">Size to be mapped</param>
|
|
||||||
private void AddMapping(ulong va, ulong size)
|
|
||||||
{
|
|
||||||
int pages = GetPagesCount(va, size, out _);
|
|
||||||
ulong pageStart = va >> PageBits;
|
|
||||||
ulong pageEnd = pageStart + (ulong)pages;
|
|
||||||
|
|
||||||
GetPageBlockRange(pageStart, pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex);
|
|
||||||
|
|
||||||
ulong mask = startMask;
|
|
||||||
|
|
||||||
while (pageIndex <= pageEndIndex)
|
|
||||||
{
|
|
||||||
if (pageIndex == pageEndIndex)
|
|
||||||
{
|
|
||||||
mask &= endMask;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
|
||||||
|
|
||||||
ulong pte;
|
|
||||||
ulong mappedMask;
|
|
||||||
|
|
||||||
// Map all 2-bit entries that are unmapped.
|
|
||||||
do
|
|
||||||
{
|
|
||||||
pte = Volatile.Read(ref pageRef);
|
|
||||||
|
|
||||||
mappedMask = pte | (pte >> 1);
|
|
||||||
mappedMask |= (mappedMask & BlockMappedMask) << 1;
|
|
||||||
mappedMask |= ~mask; // Treat everything outside the range as mapped, thus unchanged.
|
|
||||||
}
|
|
||||||
while (Interlocked.CompareExchange(ref pageRef, (pte & mappedMask) | (BlockMappedMask & (~mappedMask)), pte) != pte);
|
|
||||||
|
|
||||||
mask = ulong.MaxValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Removes the given address mapping from the page table.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="va">Virtual memory address</param>
|
|
||||||
/// <param name="size">Size to be unmapped</param>
|
|
||||||
private void RemoveMapping(ulong va, ulong size)
|
|
||||||
{
|
|
||||||
int pages = GetPagesCount(va, size, out _);
|
|
||||||
ulong pageStart = va >> PageBits;
|
|
||||||
ulong pageEnd = pageStart + (ulong)pages;
|
|
||||||
|
|
||||||
GetPageBlockRange(pageStart, pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex);
|
|
||||||
|
|
||||||
startMask = ~startMask;
|
|
||||||
endMask = ~endMask;
|
|
||||||
|
|
||||||
ulong mask = startMask;
|
|
||||||
|
|
||||||
while (pageIndex <= pageEndIndex)
|
|
||||||
{
|
|
||||||
if (pageIndex == pageEndIndex)
|
|
||||||
{
|
|
||||||
mask |= endMask;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
|
||||||
ulong pte;
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
pte = Volatile.Read(ref pageRef);
|
|
||||||
}
|
|
||||||
while (Interlocked.CompareExchange(ref pageRef, pte & mask, pte) != pte);
|
|
||||||
|
|
||||||
mask = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private ulong GetPhysicalAddressChecked(ulong va)
|
private ulong GetPhysicalAddressChecked(ulong va)
|
||||||
{
|
{
|
||||||
if (!IsMapped(va))
|
if (!IsMapped(va))
|
||||||
|
@ -936,6 +558,10 @@ namespace Ryujinx.Cpu.AppleHv
|
||||||
_addressSpace.Dispose();
|
_addressSpace.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ThrowInvalidMemoryRegionException(string message) => throw new InvalidMemoryRegionException(message);
|
protected override Span<byte> GetPhysicalAddressSpan(ulong pa, int size)
|
||||||
|
=> _backingMemory.GetSpan(pa, size);
|
||||||
|
|
||||||
|
protected override ulong TranslateVirtualAddressForRead(ulong va)
|
||||||
|
=> GetPhysicalAddressChecked(va);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,8 +28,9 @@ namespace Ryujinx.Cpu
|
||||||
/// <param name="address">CPU virtual address of the region</param>
|
/// <param name="address">CPU virtual address of the region</param>
|
||||||
/// <param name="size">Size of the region</param>
|
/// <param name="size">Size of the region</param>
|
||||||
/// <param name="id">Handle ID</param>
|
/// <param name="id">Handle ID</param>
|
||||||
|
/// <param name="flags">Region flags</param>
|
||||||
/// <returns>The memory tracking handle</returns>
|
/// <returns>The memory tracking handle</returns>
|
||||||
RegionHandle BeginTracking(ulong address, ulong size, int id);
|
RegionHandle BeginTracking(ulong address, ulong size, int id, RegionFlags flags = RegionFlags.None);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Obtains a memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
|
/// Obtains a memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
|
||||||
|
@ -39,8 +40,9 @@ namespace Ryujinx.Cpu
|
||||||
/// <param name="handles">Handles to inherit state from or reuse. When none are present, provide null</param>
|
/// <param name="handles">Handles to inherit state from or reuse. When none are present, provide null</param>
|
||||||
/// <param name="granularity">Desired granularity of write tracking</param>
|
/// <param name="granularity">Desired granularity of write tracking</param>
|
||||||
/// <param name="id">Handle ID</param>
|
/// <param name="id">Handle ID</param>
|
||||||
|
/// <param name="flags">Region flags</param>
|
||||||
/// <returns>The memory tracking handle</returns>
|
/// <returns>The memory tracking handle</returns>
|
||||||
MultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity, int id);
|
MultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity, int id, RegionFlags flags = RegionFlags.None);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Obtains a smart memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
|
/// Obtains a smart memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
|
||||||
|
|
|
@ -14,12 +14,8 @@ namespace Ryujinx.Cpu.Jit
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a CPU memory manager.
|
/// Represents a CPU memory manager.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class MemoryManager : MemoryManagerBase, IMemoryManager, IVirtualMemoryManagerTracked, IWritableBlock
|
public sealed class MemoryManager : VirtualMemoryManagerRefCountedBase<ulong, ulong>, IMemoryManager, IVirtualMemoryManagerTracked, IWritableBlock
|
||||||
{
|
{
|
||||||
public const int PageBits = 12;
|
|
||||||
public const int PageSize = 1 << PageBits;
|
|
||||||
public const int PageMask = PageSize - 1;
|
|
||||||
|
|
||||||
private const int PteSize = 8;
|
private const int PteSize = 8;
|
||||||
|
|
||||||
private const int PointerTagBit = 62;
|
private const int PointerTagBit = 62;
|
||||||
|
@ -35,10 +31,10 @@ namespace Ryujinx.Cpu.Jit
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int AddressSpaceBits { get; }
|
public int AddressSpaceBits { get; }
|
||||||
|
|
||||||
private readonly ulong _addressSpaceSize;
|
|
||||||
|
|
||||||
private readonly MemoryBlock _pageTable;
|
private readonly MemoryBlock _pageTable;
|
||||||
|
|
||||||
|
private readonly ManagedPageFlags _pages;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Page table base pointer.
|
/// Page table base pointer.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -50,6 +46,8 @@ namespace Ryujinx.Cpu.Jit
|
||||||
|
|
||||||
public event Action<ulong, ulong> UnmapEvent;
|
public event Action<ulong, ulong> UnmapEvent;
|
||||||
|
|
||||||
|
protected override ulong AddressSpaceSize { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of the memory manager.
|
/// Creates a new instance of the memory manager.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -71,9 +69,11 @@ namespace Ryujinx.Cpu.Jit
|
||||||
}
|
}
|
||||||
|
|
||||||
AddressSpaceBits = asBits;
|
AddressSpaceBits = asBits;
|
||||||
_addressSpaceSize = asSize;
|
AddressSpaceSize = asSize;
|
||||||
_pageTable = new MemoryBlock((asSize / PageSize) * PteSize);
|
_pageTable = new MemoryBlock((asSize / PageSize) * PteSize);
|
||||||
|
|
||||||
|
_pages = new ManagedPageFlags(AddressSpaceBits);
|
||||||
|
|
||||||
Tracking = new MemoryTracking(this, PageSize);
|
Tracking = new MemoryTracking(this, PageSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,6 +93,7 @@ namespace Ryujinx.Cpu.Jit
|
||||||
remainingSize -= PageSize;
|
remainingSize -= PageSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_pages.AddMapping(oVa, size);
|
||||||
Tracking.Map(oVa, size);
|
Tracking.Map(oVa, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,6 +116,7 @@ namespace Ryujinx.Cpu.Jit
|
||||||
|
|
||||||
UnmapEvent?.Invoke(va, size);
|
UnmapEvent?.Invoke(va, size);
|
||||||
Tracking.Unmap(va, size);
|
Tracking.Unmap(va, size);
|
||||||
|
_pages.RemoveMapping(va, size);
|
||||||
|
|
||||||
ulong remainingSize = size;
|
ulong remainingSize = size;
|
||||||
while (remainingSize != 0)
|
while (remainingSize != 0)
|
||||||
|
@ -153,9 +155,39 @@ namespace Ryujinx.Cpu.Jit
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public void Read(ulong va, Span<byte> data)
|
public T ReadGuest<T>(ulong va) where T : unmanaged
|
||||||
{
|
{
|
||||||
ReadImpl(va, data);
|
try
|
||||||
|
{
|
||||||
|
SignalMemoryTrackingImpl(va, (ulong)Unsafe.SizeOf<T>(), false, true);
|
||||||
|
|
||||||
|
return Read<T>(va);
|
||||||
|
}
|
||||||
|
catch (InvalidMemoryRegionException)
|
||||||
|
{
|
||||||
|
if (_invalidAccessHandler == null || !_invalidAccessHandler(va))
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public override void Read(ulong va, Span<byte> data)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
base.Read(va, data);
|
||||||
|
}
|
||||||
|
catch (InvalidMemoryRegionException)
|
||||||
|
{
|
||||||
|
if (_invalidAccessHandler == null || !_invalidAccessHandler(va))
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
@ -177,6 +209,16 @@ namespace Ryujinx.Cpu.Jit
|
||||||
WriteImpl(va, data);
|
WriteImpl(va, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public void WriteGuest<T>(ulong va, T value) where T : unmanaged
|
||||||
|
{
|
||||||
|
Span<byte> data = MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateSpan(ref value, 1));
|
||||||
|
|
||||||
|
SignalMemoryTrackingImpl(va, (ulong)data.Length, true, true);
|
||||||
|
|
||||||
|
WriteImpl(va, data);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public void WriteUntracked(ulong va, ReadOnlySpan<byte> data)
|
public void WriteUntracked(ulong va, ReadOnlySpan<byte> data)
|
||||||
{
|
{
|
||||||
|
@ -290,7 +332,7 @@ namespace Ryujinx.Cpu.Jit
|
||||||
{
|
{
|
||||||
Span<byte> data = new byte[size];
|
Span<byte> data = new byte[size];
|
||||||
|
|
||||||
ReadImpl(va, data);
|
base.Read(va, data);
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
@ -462,48 +504,6 @@ namespace Ryujinx.Cpu.Jit
|
||||||
return regions;
|
return regions;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ReadImpl(ulong va, Span<byte> data)
|
|
||||||
{
|
|
||||||
if (data.Length == 0)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
AssertValidAddressAndSize(va, (ulong)data.Length);
|
|
||||||
|
|
||||||
int offset = 0, size;
|
|
||||||
|
|
||||||
if ((va & PageMask) != 0)
|
|
||||||
{
|
|
||||||
ulong pa = GetPhysicalAddressInternal(va);
|
|
||||||
|
|
||||||
size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
|
|
||||||
|
|
||||||
_backingMemory.GetSpan(pa, size).CopyTo(data[..size]);
|
|
||||||
|
|
||||||
offset += size;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (; offset < data.Length; offset += size)
|
|
||||||
{
|
|
||||||
ulong pa = GetPhysicalAddressInternal(va + (ulong)offset);
|
|
||||||
|
|
||||||
size = Math.Min(data.Length - offset, PageSize);
|
|
||||||
|
|
||||||
_backingMemory.GetSpan(pa, size).CopyTo(data.Slice(offset, size));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (InvalidMemoryRegionException)
|
|
||||||
{
|
|
||||||
if (_invalidAccessHandler == null || !_invalidAccessHandler(va))
|
|
||||||
{
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public bool IsRangeMapped(ulong va, ulong size)
|
public bool IsRangeMapped(ulong va, ulong size)
|
||||||
{
|
{
|
||||||
|
@ -544,37 +544,6 @@ namespace Ryujinx.Cpu.Jit
|
||||||
return _pageTable.Read<ulong>((va / PageSize) * PteSize) != 0;
|
return _pageTable.Read<ulong>((va / PageSize) * PteSize) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool ValidateAddress(ulong va)
|
|
||||||
{
|
|
||||||
return va < _addressSpaceSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks if the combination of virtual address and size is part of the addressable space.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="va">Virtual address of the range</param>
|
|
||||||
/// <param name="size">Size of the range in bytes</param>
|
|
||||||
/// <returns>True if the combination of virtual address and size is part of the addressable space</returns>
|
|
||||||
private bool ValidateAddressAndSize(ulong va, ulong size)
|
|
||||||
{
|
|
||||||
ulong endVa = va + size;
|
|
||||||
return endVa >= va && endVa >= size && endVa <= _addressSpaceSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Ensures the combination of virtual address and size is part of the addressable space.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="va">Virtual address of the range</param>
|
|
||||||
/// <param name="size">Size of the range in bytes</param>
|
|
||||||
/// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified outside the addressable space</exception>
|
|
||||||
private void AssertValidAddressAndSize(ulong va, ulong size)
|
|
||||||
{
|
|
||||||
if (!ValidateAddressAndSize(va, size))
|
|
||||||
{
|
|
||||||
throw new InvalidMemoryRegionException($"va=0x{va:X16}, size=0x{size:X16}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private ulong GetPhysicalAddressInternal(ulong va)
|
private ulong GetPhysicalAddressInternal(ulong va)
|
||||||
{
|
{
|
||||||
return PteToPa(_pageTable.Read<ulong>((va / PageSize) * PteSize) & ~(0xffffUL << 48)) + (va & PageMask);
|
return PteToPa(_pageTable.Read<ulong>((va / PageSize) * PteSize) & ~(0xffffUL << 48)) + (va & PageMask);
|
||||||
|
@ -587,10 +556,12 @@ namespace Ryujinx.Cpu.Jit
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public void TrackingReprotect(ulong va, ulong size, MemoryPermission protection)
|
public void TrackingReprotect(ulong va, ulong size, MemoryPermission protection, bool guest)
|
||||||
{
|
{
|
||||||
AssertValidAddressAndSize(va, size);
|
AssertValidAddressAndSize(va, size);
|
||||||
|
|
||||||
|
if (guest)
|
||||||
|
{
|
||||||
// Protection is inverted on software pages, since the default value is 0.
|
// Protection is inverted on software pages, since the default value is 0.
|
||||||
protection = (~protection) & MemoryPermission.ReadAndWrite;
|
protection = (~protection) & MemoryPermission.ReadAndWrite;
|
||||||
|
|
||||||
|
@ -620,17 +591,22 @@ namespace Ryujinx.Cpu.Jit
|
||||||
pageStart++;
|
pageStart++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
/// <inheritdoc/>
|
|
||||||
public RegionHandle BeginTracking(ulong address, ulong size, int id)
|
|
||||||
{
|
{
|
||||||
return Tracking.BeginTracking(address, size, id);
|
_pages.TrackingReprotect(va, size, protection);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public MultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity, int id)
|
public RegionHandle BeginTracking(ulong address, ulong size, int id, RegionFlags flags = RegionFlags.None)
|
||||||
{
|
{
|
||||||
return Tracking.BeginGranularTracking(address, size, handles, granularity, id);
|
return Tracking.BeginTracking(address, size, id, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public MultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity, int id, RegionFlags flags = RegionFlags.None)
|
||||||
|
{
|
||||||
|
return Tracking.BeginGranularTracking(address, size, handles, granularity, id, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
@ -639,8 +615,7 @@ namespace Ryujinx.Cpu.Jit
|
||||||
return Tracking.BeginSmartGranularTracking(address, size, granularity, id);
|
return Tracking.BeginSmartGranularTracking(address, size, granularity, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
private void SignalMemoryTrackingImpl(ulong va, ulong size, bool write, bool guest, bool precise = false, int? exemptId = null)
|
||||||
public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false, int? exemptId = null)
|
|
||||||
{
|
{
|
||||||
AssertValidAddressAndSize(va, size);
|
AssertValidAddressAndSize(va, size);
|
||||||
|
|
||||||
|
@ -650,6 +625,11 @@ namespace Ryujinx.Cpu.Jit
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the memory tracking is coming from the guest, use the tag bits in the page table entry.
|
||||||
|
// Otherwise, use the managed page flags.
|
||||||
|
|
||||||
|
if (guest)
|
||||||
|
{
|
||||||
// We emulate guard pages for software memory access. This makes for an easy transition to
|
// We emulate guard pages for software memory access. This makes for an easy transition to
|
||||||
// tracking using host guard pages in future, but also supporting platforms where this is not possible.
|
// tracking using host guard pages in future, but also supporting platforms where this is not possible.
|
||||||
|
|
||||||
|
@ -669,13 +649,24 @@ namespace Ryujinx.Cpu.Jit
|
||||||
|
|
||||||
if ((pte & tag) != 0)
|
if ((pte & tag) != 0)
|
||||||
{
|
{
|
||||||
Tracking.VirtualMemoryEvent(va, size, write, precise: false, exemptId);
|
Tracking.VirtualMemoryEvent(va, size, write, precise: false, exemptId, true);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
pageStart++;
|
pageStart++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_pages.SignalMemoryTracking(Tracking, va, size, write, exemptId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public void SignalMemoryTracking(ulong va, ulong size, bool write, bool precise = false, int? exemptId = null)
|
||||||
|
{
|
||||||
|
SignalMemoryTrackingImpl(va, size, write, false, precise, exemptId);
|
||||||
|
}
|
||||||
|
|
||||||
private ulong PaToPte(ulong pa)
|
private ulong PaToPte(ulong pa)
|
||||||
{
|
{
|
||||||
|
@ -691,5 +682,11 @@ namespace Ryujinx.Cpu.Jit
|
||||||
/// Disposes of resources used by the memory manager.
|
/// Disposes of resources used by the memory manager.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected override void Destroy() => _pageTable.Dispose();
|
protected override void Destroy() => _pageTable.Dispose();
|
||||||
|
|
||||||
|
protected override Span<byte> GetPhysicalAddressSpan(ulong pa, int size)
|
||||||
|
=> _backingMemory.GetSpan(pa, size);
|
||||||
|
|
||||||
|
protected override ulong TranslateVirtualAddressForRead(ulong va)
|
||||||
|
=> GetPhysicalAddressInternal(va);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,46 +6,24 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace Ryujinx.Cpu.Jit
|
namespace Ryujinx.Cpu.Jit
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a CPU memory manager which maps guest virtual memory directly onto a host virtual region.
|
/// Represents a CPU memory manager which maps guest virtual memory directly onto a host virtual region.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class MemoryManagerHostMapped : MemoryManagerBase, IMemoryManager, IVirtualMemoryManagerTracked, IWritableBlock
|
public sealed class MemoryManagerHostMapped : VirtualMemoryManagerRefCountedBase<ulong, ulong>, IMemoryManager, IVirtualMemoryManagerTracked, IWritableBlock
|
||||||
{
|
{
|
||||||
public const int PageBits = 12;
|
|
||||||
public const int PageSize = 1 << PageBits;
|
|
||||||
public const int PageMask = PageSize - 1;
|
|
||||||
|
|
||||||
public const int PageToPteShift = 5; // 32 pages (2 bits each) in one ulong page table entry.
|
|
||||||
public const ulong BlockMappedMask = 0x5555555555555555; // First bit of each table entry set.
|
|
||||||
|
|
||||||
private enum HostMappedPtBits : ulong
|
|
||||||
{
|
|
||||||
Unmapped = 0,
|
|
||||||
Mapped,
|
|
||||||
WriteTracked,
|
|
||||||
ReadWriteTracked,
|
|
||||||
|
|
||||||
MappedReplicated = 0x5555555555555555,
|
|
||||||
WriteTrackedReplicated = 0xaaaaaaaaaaaaaaaa,
|
|
||||||
ReadWriteTrackedReplicated = ulong.MaxValue,
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly InvalidAccessHandler _invalidAccessHandler;
|
private readonly InvalidAccessHandler _invalidAccessHandler;
|
||||||
private readonly bool _unsafeMode;
|
private readonly bool _unsafeMode;
|
||||||
|
|
||||||
private readonly AddressSpace _addressSpace;
|
private readonly AddressSpace _addressSpace;
|
||||||
|
|
||||||
public ulong AddressSpaceSize { get; }
|
|
||||||
|
|
||||||
private readonly PageTable<ulong> _pageTable;
|
private readonly PageTable<ulong> _pageTable;
|
||||||
|
|
||||||
private readonly MemoryEhMeilleure _memoryEh;
|
private readonly MemoryEhMeilleure _memoryEh;
|
||||||
|
|
||||||
private readonly ulong[] _pageBitmap;
|
private readonly ManagedPageFlags _pages;
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public bool Supports4KBPages => MemoryBlock.GetPageSize() == PageSize;
|
public bool Supports4KBPages => MemoryBlock.GetPageSize() == PageSize;
|
||||||
|
@ -60,6 +38,8 @@ namespace Ryujinx.Cpu.Jit
|
||||||
|
|
||||||
public event Action<ulong, ulong> UnmapEvent;
|
public event Action<ulong, ulong> UnmapEvent;
|
||||||
|
|
||||||
|
protected override ulong AddressSpaceSize { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of the host mapped memory manager.
|
/// Creates a new instance of the host mapped memory manager.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -85,48 +65,12 @@ namespace Ryujinx.Cpu.Jit
|
||||||
|
|
||||||
AddressSpaceBits = asBits;
|
AddressSpaceBits = asBits;
|
||||||
|
|
||||||
_pageBitmap = new ulong[1 << (AddressSpaceBits - (PageBits + PageToPteShift))];
|
_pages = new ManagedPageFlags(AddressSpaceBits);
|
||||||
|
|
||||||
Tracking = new MemoryTracking(this, (int)MemoryBlock.GetPageSize(), invalidAccessHandler);
|
Tracking = new MemoryTracking(this, (int)MemoryBlock.GetPageSize(), invalidAccessHandler);
|
||||||
_memoryEh = new MemoryEhMeilleure(_addressSpace.Base, _addressSpace.Mirror, Tracking);
|
_memoryEh = new MemoryEhMeilleure(_addressSpace.Base, _addressSpace.Mirror, Tracking);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks if the virtual address is part of the addressable space.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="va">Virtual address</param>
|
|
||||||
/// <returns>True if the virtual address is part of the addressable space</returns>
|
|
||||||
private bool ValidateAddress(ulong va)
|
|
||||||
{
|
|
||||||
return va < AddressSpaceSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks if the combination of virtual address and size is part of the addressable space.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="va">Virtual address of the range</param>
|
|
||||||
/// <param name="size">Size of the range in bytes</param>
|
|
||||||
/// <returns>True if the combination of virtual address and size is part of the addressable space</returns>
|
|
||||||
private bool ValidateAddressAndSize(ulong va, ulong size)
|
|
||||||
{
|
|
||||||
ulong endVa = va + size;
|
|
||||||
return endVa >= va && endVa >= size && endVa <= AddressSpaceSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Ensures the combination of virtual address and size is part of the addressable space.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="va">Virtual address of the range</param>
|
|
||||||
/// <param name="size">Size of the range in bytes</param>
|
|
||||||
/// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified outside the addressable space</exception>
|
|
||||||
private void AssertValidAddressAndSize(ulong va, ulong size)
|
|
||||||
{
|
|
||||||
if (!ValidateAddressAndSize(va, size))
|
|
||||||
{
|
|
||||||
throw new InvalidMemoryRegionException($"va=0x{va:X16}, size=0x{size:X16}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ensures the combination of virtual address and size is part of the addressable space and fully mapped.
|
/// Ensures the combination of virtual address and size is part of the addressable space and fully mapped.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -134,7 +78,7 @@ namespace Ryujinx.Cpu.Jit
|
||||||
/// <param name="size">Size of the range in bytes</param>
|
/// <param name="size">Size of the range in bytes</param>
|
||||||
private void AssertMapped(ulong va, ulong size)
|
private void AssertMapped(ulong va, ulong size)
|
||||||
{
|
{
|
||||||
if (!ValidateAddressAndSize(va, size) || !IsRangeMappedImpl(va, size))
|
if (!ValidateAddressAndSize(va, size) || !_pages.IsRangeMapped(va, size))
|
||||||
{
|
{
|
||||||
throw new InvalidMemoryRegionException($"Not mapped: va=0x{va:X16}, size=0x{size:X16}");
|
throw new InvalidMemoryRegionException($"Not mapped: va=0x{va:X16}, size=0x{size:X16}");
|
||||||
}
|
}
|
||||||
|
@ -146,7 +90,7 @@ namespace Ryujinx.Cpu.Jit
|
||||||
AssertValidAddressAndSize(va, size);
|
AssertValidAddressAndSize(va, size);
|
||||||
|
|
||||||
_addressSpace.Map(va, pa, size, flags);
|
_addressSpace.Map(va, pa, size, flags);
|
||||||
AddMapping(va, size);
|
_pages.AddMapping(va, size);
|
||||||
PtMap(va, pa, size);
|
PtMap(va, pa, size);
|
||||||
|
|
||||||
Tracking.Map(va, size);
|
Tracking.Map(va, size);
|
||||||
|
@ -166,7 +110,7 @@ namespace Ryujinx.Cpu.Jit
|
||||||
UnmapEvent?.Invoke(va, size);
|
UnmapEvent?.Invoke(va, size);
|
||||||
Tracking.Unmap(va, size);
|
Tracking.Unmap(va, size);
|
||||||
|
|
||||||
RemoveMapping(va, size);
|
_pages.RemoveMapping(va, size);
|
||||||
PtUnmap(va, size);
|
PtUnmap(va, size);
|
||||||
_addressSpace.Unmap(va, size);
|
_addressSpace.Unmap(va, size);
|
||||||
}
|
}
|
||||||
|
@ -235,7 +179,7 @@ namespace Ryujinx.Cpu.Jit
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public void Read(ulong va, Span<byte> data)
|
public override void Read(ulong va, Span<byte> data)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -377,22 +321,7 @@ namespace Ryujinx.Cpu.Jit
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public bool IsMapped(ulong va)
|
public bool IsMapped(ulong va)
|
||||||
{
|
{
|
||||||
return ValidateAddress(va) && IsMappedImpl(va);
|
return ValidateAddress(va) && _pages.IsMapped(va);
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
private bool IsMappedImpl(ulong va)
|
|
||||||
{
|
|
||||||
ulong page = va >> PageBits;
|
|
||||||
|
|
||||||
int bit = (int)((page & 31) << 1);
|
|
||||||
|
|
||||||
int pageIndex = (int)(page >> PageToPteShift);
|
|
||||||
ref ulong pageRef = ref _pageBitmap[pageIndex];
|
|
||||||
|
|
||||||
ulong pte = Volatile.Read(ref pageRef);
|
|
||||||
|
|
||||||
return ((pte >> bit) & 3) != 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
@ -400,58 +329,7 @@ namespace Ryujinx.Cpu.Jit
|
||||||
{
|
{
|
||||||
AssertValidAddressAndSize(va, size);
|
AssertValidAddressAndSize(va, size);
|
||||||
|
|
||||||
return IsRangeMappedImpl(va, size);
|
return _pages.IsRangeMapped(va, size);
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
private static void GetPageBlockRange(ulong pageStart, ulong pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex)
|
|
||||||
{
|
|
||||||
startMask = ulong.MaxValue << ((int)(pageStart & 31) << 1);
|
|
||||||
endMask = ulong.MaxValue >> (64 - ((int)(pageEnd & 31) << 1));
|
|
||||||
|
|
||||||
pageIndex = (int)(pageStart >> PageToPteShift);
|
|
||||||
pageEndIndex = (int)((pageEnd - 1) >> PageToPteShift);
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool IsRangeMappedImpl(ulong va, ulong size)
|
|
||||||
{
|
|
||||||
int pages = GetPagesCount(va, size, out _);
|
|
||||||
|
|
||||||
if (pages == 1)
|
|
||||||
{
|
|
||||||
return IsMappedImpl(va);
|
|
||||||
}
|
|
||||||
|
|
||||||
ulong pageStart = va >> PageBits;
|
|
||||||
ulong pageEnd = pageStart + (ulong)pages;
|
|
||||||
|
|
||||||
GetPageBlockRange(pageStart, pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex);
|
|
||||||
|
|
||||||
// Check if either bit in each 2 bit page entry is set.
|
|
||||||
// OR the block with itself shifted down by 1, and check the first bit of each entry.
|
|
||||||
|
|
||||||
ulong mask = BlockMappedMask & startMask;
|
|
||||||
|
|
||||||
while (pageIndex <= pageEndIndex)
|
|
||||||
{
|
|
||||||
if (pageIndex == pageEndIndex)
|
|
||||||
{
|
|
||||||
mask &= endMask;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
|
||||||
ulong pte = Volatile.Read(ref pageRef);
|
|
||||||
|
|
||||||
pte |= pte >> 1;
|
|
||||||
if ((pte & mask) != mask)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
mask = BlockMappedMask;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
@ -526,76 +404,7 @@ namespace Ryujinx.Cpu.Jit
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Software table, used for managed memory tracking.
|
_pages.SignalMemoryTracking(Tracking, va, size, write, exemptId);
|
||||||
|
|
||||||
int pages = GetPagesCount(va, size, out _);
|
|
||||||
ulong pageStart = va >> PageBits;
|
|
||||||
|
|
||||||
if (pages == 1)
|
|
||||||
{
|
|
||||||
ulong tag = (ulong)(write ? HostMappedPtBits.WriteTracked : HostMappedPtBits.ReadWriteTracked);
|
|
||||||
|
|
||||||
int bit = (int)((pageStart & 31) << 1);
|
|
||||||
|
|
||||||
int pageIndex = (int)(pageStart >> PageToPteShift);
|
|
||||||
ref ulong pageRef = ref _pageBitmap[pageIndex];
|
|
||||||
|
|
||||||
ulong pte = Volatile.Read(ref pageRef);
|
|
||||||
ulong state = ((pte >> bit) & 3);
|
|
||||||
|
|
||||||
if (state >= tag)
|
|
||||||
{
|
|
||||||
Tracking.VirtualMemoryEvent(va, size, write, precise: false, exemptId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else if (state == 0)
|
|
||||||
{
|
|
||||||
ThrowInvalidMemoryRegionException($"Not mapped: va=0x{va:X16}, size=0x{size:X16}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ulong pageEnd = pageStart + (ulong)pages;
|
|
||||||
|
|
||||||
GetPageBlockRange(pageStart, pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex);
|
|
||||||
|
|
||||||
ulong mask = startMask;
|
|
||||||
|
|
||||||
ulong anyTrackingTag = (ulong)HostMappedPtBits.WriteTrackedReplicated;
|
|
||||||
|
|
||||||
while (pageIndex <= pageEndIndex)
|
|
||||||
{
|
|
||||||
if (pageIndex == pageEndIndex)
|
|
||||||
{
|
|
||||||
mask &= endMask;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
|
||||||
|
|
||||||
ulong pte = Volatile.Read(ref pageRef);
|
|
||||||
ulong mappedMask = mask & BlockMappedMask;
|
|
||||||
|
|
||||||
ulong mappedPte = pte | (pte >> 1);
|
|
||||||
if ((mappedPte & mappedMask) != mappedMask)
|
|
||||||
{
|
|
||||||
ThrowInvalidMemoryRegionException($"Not mapped: va=0x{va:X16}, size=0x{size:X16}");
|
|
||||||
}
|
|
||||||
|
|
||||||
pte &= mask;
|
|
||||||
if ((pte & anyTrackingTag) != 0) // Search for any tracking.
|
|
||||||
{
|
|
||||||
// Writes trigger any tracking.
|
|
||||||
// Only trigger tracking from reads if both bits are set on any page.
|
|
||||||
if (write || (pte & (pte >> 1) & BlockMappedMask) != 0)
|
|
||||||
{
|
|
||||||
Tracking.VirtualMemoryEvent(va, size, write, precise: false, exemptId);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mask = ulong.MaxValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -622,103 +431,28 @@ namespace Ryujinx.Cpu.Jit
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public void TrackingReprotect(ulong va, ulong size, MemoryPermission protection)
|
public void TrackingReprotect(ulong va, ulong size, MemoryPermission protection, bool guest)
|
||||||
{
|
{
|
||||||
// Protection is inverted on software pages, since the default value is 0.
|
if (guest)
|
||||||
protection = (~protection) & MemoryPermission.ReadAndWrite;
|
|
||||||
|
|
||||||
int pages = GetPagesCount(va, size, out va);
|
|
||||||
ulong pageStart = va >> PageBits;
|
|
||||||
|
|
||||||
if (pages == 1)
|
|
||||||
{
|
{
|
||||||
ulong protTag = protection switch
|
_addressSpace.Base.Reprotect(va, size, protection, false);
|
||||||
{
|
|
||||||
MemoryPermission.None => (ulong)HostMappedPtBits.Mapped,
|
|
||||||
MemoryPermission.Write => (ulong)HostMappedPtBits.WriteTracked,
|
|
||||||
_ => (ulong)HostMappedPtBits.ReadWriteTracked,
|
|
||||||
};
|
|
||||||
|
|
||||||
int bit = (int)((pageStart & 31) << 1);
|
|
||||||
|
|
||||||
ulong tagMask = 3UL << bit;
|
|
||||||
ulong invTagMask = ~tagMask;
|
|
||||||
|
|
||||||
ulong tag = protTag << bit;
|
|
||||||
|
|
||||||
int pageIndex = (int)(pageStart >> PageToPteShift);
|
|
||||||
ref ulong pageRef = ref _pageBitmap[pageIndex];
|
|
||||||
|
|
||||||
ulong pte;
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
pte = Volatile.Read(ref pageRef);
|
|
||||||
}
|
|
||||||
while ((pte & tagMask) != 0 && Interlocked.CompareExchange(ref pageRef, (pte & invTagMask) | tag, pte) != pte);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ulong pageEnd = pageStart + (ulong)pages;
|
_pages.TrackingReprotect(va, size, protection);
|
||||||
|
|
||||||
GetPageBlockRange(pageStart, pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex);
|
|
||||||
|
|
||||||
ulong mask = startMask;
|
|
||||||
|
|
||||||
ulong protTag = protection switch
|
|
||||||
{
|
|
||||||
MemoryPermission.None => (ulong)HostMappedPtBits.MappedReplicated,
|
|
||||||
MemoryPermission.Write => (ulong)HostMappedPtBits.WriteTrackedReplicated,
|
|
||||||
_ => (ulong)HostMappedPtBits.ReadWriteTrackedReplicated,
|
|
||||||
};
|
|
||||||
|
|
||||||
while (pageIndex <= pageEndIndex)
|
|
||||||
{
|
|
||||||
if (pageIndex == pageEndIndex)
|
|
||||||
{
|
|
||||||
mask &= endMask;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
|
||||||
|
|
||||||
ulong pte;
|
|
||||||
ulong mappedMask;
|
|
||||||
|
|
||||||
// Change the protection of all 2 bit entries that are mapped.
|
|
||||||
do
|
|
||||||
{
|
|
||||||
pte = Volatile.Read(ref pageRef);
|
|
||||||
|
|
||||||
mappedMask = pte | (pte >> 1);
|
|
||||||
mappedMask |= (mappedMask & BlockMappedMask) << 1;
|
|
||||||
mappedMask &= mask; // Only update mapped pages within the given range.
|
|
||||||
}
|
|
||||||
while (Interlocked.CompareExchange(ref pageRef, (pte & (~mappedMask)) | (protTag & mappedMask), pte) != pte);
|
|
||||||
|
|
||||||
mask = ulong.MaxValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protection = protection switch
|
|
||||||
{
|
|
||||||
MemoryPermission.None => MemoryPermission.ReadAndWrite,
|
|
||||||
MemoryPermission.Write => MemoryPermission.Read,
|
|
||||||
_ => MemoryPermission.None,
|
|
||||||
};
|
|
||||||
|
|
||||||
_addressSpace.Base.Reprotect(va, size, protection, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public RegionHandle BeginTracking(ulong address, ulong size, int id)
|
public RegionHandle BeginTracking(ulong address, ulong size, int id, RegionFlags flags = RegionFlags.None)
|
||||||
{
|
{
|
||||||
return Tracking.BeginTracking(address, size, id);
|
return Tracking.BeginTracking(address, size, id, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public MultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity, int id)
|
public MultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity, int id, RegionFlags flags = RegionFlags.None)
|
||||||
{
|
{
|
||||||
return Tracking.BeginGranularTracking(address, size, handles, granularity, id);
|
return Tracking.BeginGranularTracking(address, size, handles, granularity, id, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
@ -727,86 +461,6 @@ namespace Ryujinx.Cpu.Jit
|
||||||
return Tracking.BeginSmartGranularTracking(address, size, granularity, id);
|
return Tracking.BeginSmartGranularTracking(address, size, granularity, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds the given address mapping to the page table.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="va">Virtual memory address</param>
|
|
||||||
/// <param name="size">Size to be mapped</param>
|
|
||||||
private void AddMapping(ulong va, ulong size)
|
|
||||||
{
|
|
||||||
int pages = GetPagesCount(va, size, out _);
|
|
||||||
ulong pageStart = va >> PageBits;
|
|
||||||
ulong pageEnd = pageStart + (ulong)pages;
|
|
||||||
|
|
||||||
GetPageBlockRange(pageStart, pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex);
|
|
||||||
|
|
||||||
ulong mask = startMask;
|
|
||||||
|
|
||||||
while (pageIndex <= pageEndIndex)
|
|
||||||
{
|
|
||||||
if (pageIndex == pageEndIndex)
|
|
||||||
{
|
|
||||||
mask &= endMask;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
|
||||||
|
|
||||||
ulong pte;
|
|
||||||
ulong mappedMask;
|
|
||||||
|
|
||||||
// Map all 2-bit entries that are unmapped.
|
|
||||||
do
|
|
||||||
{
|
|
||||||
pte = Volatile.Read(ref pageRef);
|
|
||||||
|
|
||||||
mappedMask = pte | (pte >> 1);
|
|
||||||
mappedMask |= (mappedMask & BlockMappedMask) << 1;
|
|
||||||
mappedMask |= ~mask; // Treat everything outside the range as mapped, thus unchanged.
|
|
||||||
}
|
|
||||||
while (Interlocked.CompareExchange(ref pageRef, (pte & mappedMask) | (BlockMappedMask & (~mappedMask)), pte) != pte);
|
|
||||||
|
|
||||||
mask = ulong.MaxValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Removes the given address mapping from the page table.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="va">Virtual memory address</param>
|
|
||||||
/// <param name="size">Size to be unmapped</param>
|
|
||||||
private void RemoveMapping(ulong va, ulong size)
|
|
||||||
{
|
|
||||||
int pages = GetPagesCount(va, size, out _);
|
|
||||||
ulong pageStart = va >> PageBits;
|
|
||||||
ulong pageEnd = pageStart + (ulong)pages;
|
|
||||||
|
|
||||||
GetPageBlockRange(pageStart, pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex);
|
|
||||||
|
|
||||||
startMask = ~startMask;
|
|
||||||
endMask = ~endMask;
|
|
||||||
|
|
||||||
ulong mask = startMask;
|
|
||||||
|
|
||||||
while (pageIndex <= pageEndIndex)
|
|
||||||
{
|
|
||||||
if (pageIndex == pageEndIndex)
|
|
||||||
{
|
|
||||||
mask |= endMask;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
|
||||||
ulong pte;
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
pte = Volatile.Read(ref pageRef);
|
|
||||||
}
|
|
||||||
while (Interlocked.CompareExchange(ref pageRef, pte & mask, pte) != pte);
|
|
||||||
|
|
||||||
mask = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Disposes of resources used by the memory manager.
|
/// Disposes of resources used by the memory manager.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -816,6 +470,10 @@ namespace Ryujinx.Cpu.Jit
|
||||||
_memoryEh.Dispose();
|
_memoryEh.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ThrowInvalidMemoryRegionException(string message) => throw new InvalidMemoryRegionException(message);
|
protected override Span<byte> GetPhysicalAddressSpan(ulong pa, int size)
|
||||||
|
=> _addressSpace.Mirror.GetSpan(pa, size);
|
||||||
|
|
||||||
|
protected override ulong TranslateVirtualAddressForRead(ulong va)
|
||||||
|
=> va;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1106,6 +1106,7 @@ namespace Ryujinx.Cpu.LightningJit.Arm64
|
||||||
case InstName.Mrs:
|
case InstName.Mrs:
|
||||||
case InstName.MsrImm:
|
case InstName.MsrImm:
|
||||||
case InstName.MsrReg:
|
case InstName.MsrReg:
|
||||||
|
case InstName.Sysl:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
48
src/Ryujinx.Cpu/LightningJit/Arm64/SysUtils.cs
Normal file
48
src/Ryujinx.Cpu/LightningJit/Arm64/SysUtils.cs
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace Ryujinx.Cpu.LightningJit.Arm64
|
||||||
|
{
|
||||||
|
static class SysUtils
|
||||||
|
{
|
||||||
|
public static (uint, uint, uint, uint) UnpackOp1CRnCRmOp2(uint encoding)
|
||||||
|
{
|
||||||
|
uint op1 = (encoding >> 16) & 7;
|
||||||
|
uint crn = (encoding >> 12) & 0xf;
|
||||||
|
uint crm = (encoding >> 8) & 0xf;
|
||||||
|
uint op2 = (encoding >> 5) & 7;
|
||||||
|
|
||||||
|
return (op1, crn, crm, op2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool IsCacheInstEl0(uint encoding)
|
||||||
|
{
|
||||||
|
(uint op1, uint crn, uint crm, uint op2) = UnpackOp1CRnCRmOp2(encoding);
|
||||||
|
|
||||||
|
return ((op1 << 11) | (crn << 7) | (crm << 3) | op2) switch
|
||||||
|
{
|
||||||
|
0b011_0111_0100_001 => true, // DC ZVA
|
||||||
|
0b011_0111_1010_001 => true, // DC CVAC
|
||||||
|
0b011_0111_1100_001 => true, // DC CVAP
|
||||||
|
0b011_0111_1011_001 => true, // DC CVAU
|
||||||
|
0b011_0111_1110_001 => true, // DC CIVAC
|
||||||
|
0b011_0111_0101_001 => true, // IC IVAU
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool IsCacheInstUciTrapped(uint encoding)
|
||||||
|
{
|
||||||
|
(uint op1, uint crn, uint crm, uint op2) = UnpackOp1CRnCRmOp2(encoding);
|
||||||
|
|
||||||
|
return ((op1 << 11) | (crn << 7) | (crm << 3) | op2) switch
|
||||||
|
{
|
||||||
|
0b011_0111_1010_001 => true, // DC CVAC
|
||||||
|
0b011_0111_1100_001 => true, // DC CVAP
|
||||||
|
0b011_0111_1011_001 => true, // DC CVAU
|
||||||
|
0b011_0111_1110_001 => true, // DC CIVAC
|
||||||
|
0b011_0111_0101_001 => true, // IC IVAU
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -257,7 +257,7 @@ namespace Ryujinx.Cpu.LightningJit.Arm64.Target.Arm64
|
||||||
|
|
||||||
(name, flags, AddressForm addressForm) = InstTable.GetInstNameAndFlags(encoding, cpuPreset.Version, cpuPreset.Features);
|
(name, flags, AddressForm addressForm) = InstTable.GetInstNameAndFlags(encoding, cpuPreset.Version, cpuPreset.Features);
|
||||||
|
|
||||||
if (name.IsPrivileged())
|
if (name.IsPrivileged() || (name == InstName.Sys && IsPrivilegedSys(encoding)))
|
||||||
{
|
{
|
||||||
name = InstName.UdfPermUndef;
|
name = InstName.UdfPermUndef;
|
||||||
flags = InstFlags.None;
|
flags = InstFlags.None;
|
||||||
|
@ -341,6 +341,11 @@ namespace Ryujinx.Cpu.LightningJit.Arm64.Target.Arm64
|
||||||
return new(startAddress, address, insts, !isTruncated && !name.IsException(), isTruncated, isLoopEnd);
|
return new(startAddress, address, insts, !isTruncated && !name.IsException(), isTruncated, isLoopEnd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool IsPrivilegedSys(uint encoding)
|
||||||
|
{
|
||||||
|
return !SysUtils.IsCacheInstEl0(encoding);
|
||||||
|
}
|
||||||
|
|
||||||
private static bool IsMrsNzcv(uint encoding)
|
private static bool IsMrsNzcv(uint encoding)
|
||||||
{
|
{
|
||||||
return (encoding & ~0x1fu) == 0xd53b4200u;
|
return (encoding & ~0x1fu) == 0xd53b4200u;
|
||||||
|
|
|
@ -13,6 +13,14 @@ namespace Ryujinx.Cpu.LightningJit.Arm64.Target.Arm64
|
||||||
|
|
||||||
public static void RewriteSysInstruction(int asBits, MemoryManagerType mmType, CodeWriter writer, RegisterAllocator regAlloc, uint encoding)
|
public static void RewriteSysInstruction(int asBits, MemoryManagerType mmType, CodeWriter writer, RegisterAllocator regAlloc, uint encoding)
|
||||||
{
|
{
|
||||||
|
// TODO: Handle IC instruction, it should invalidate the JIT cache.
|
||||||
|
|
||||||
|
if (InstEmitSystem.IsCacheInstForbidden(encoding))
|
||||||
|
{
|
||||||
|
// Current OS does not allow cache maintenance instructions from user mode, just do nothing.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
int rtIndex = RegisterUtils.ExtractRt(encoding);
|
int rtIndex = RegisterUtils.ExtractRt(encoding);
|
||||||
if (rtIndex == RegisterUtils.ZrIndex)
|
if (rtIndex == RegisterUtils.ZrIndex)
|
||||||
{
|
{
|
||||||
|
|
|
@ -69,7 +69,7 @@ namespace Ryujinx.Cpu.LightningJit.Arm64.Target.Arm64
|
||||||
asm.LdrRiUn(Register((int)rd), Register(regAlloc.FixedContextRegister), NativeContextOffsets.TpidrEl0Offset);
|
asm.LdrRiUn(Register((int)rd), Register(regAlloc.FixedContextRegister), NativeContextOffsets.TpidrEl0Offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ((encoding & ~0x1f) == 0xd53b0020 && IsAppleOS()) // mrs x0, ctr_el0
|
else if ((encoding & ~0x1f) == 0xd53b0020 && IsCtrEl0AccessForbidden()) // mrs x0, ctr_el0
|
||||||
{
|
{
|
||||||
uint rd = encoding & 0x1f;
|
uint rd = encoding & 0x1f;
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ namespace Ryujinx.Cpu.LightningJit.Arm64.Target.Arm64
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if ((encoding & ~0x1f) == 0xd53b0020 && IsAppleOS()) // mrs x0, ctr_el0
|
else if ((encoding & ~0x1f) == 0xd53b0020 && IsCtrEl0AccessForbidden()) // mrs x0, ctr_el0
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -127,9 +127,16 @@ namespace Ryujinx.Cpu.LightningJit.Arm64.Target.Arm64
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsAppleOS()
|
private static bool IsCtrEl0AccessForbidden()
|
||||||
{
|
{
|
||||||
return OperatingSystem.IsMacOS() || OperatingSystem.IsIOS();
|
// Only Linux allows accessing CTR_EL0 from user mode.
|
||||||
|
return OperatingSystem.IsWindows() || OperatingSystem.IsMacOS() || OperatingSystem.IsIOS();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool IsCacheInstForbidden(uint encoding)
|
||||||
|
{
|
||||||
|
// Windows does not allow the cache maintenance instructions to be used from user mode.
|
||||||
|
return OperatingSystem.IsWindows() && SysUtils.IsCacheInstUciTrapped(encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool NeedsContextStoreLoad(InstName name)
|
public static bool NeedsContextStoreLoad(InstName name)
|
||||||
|
|
389
src/Ryujinx.Cpu/ManagedPageFlags.cs
Normal file
389
src/Ryujinx.Cpu/ManagedPageFlags.cs
Normal file
|
@ -0,0 +1,389 @@
|
||||||
|
using Ryujinx.Memory;
|
||||||
|
using Ryujinx.Memory.Tracking;
|
||||||
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Ryujinx.Cpu
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A page bitmap that keeps track of mapped state and tracking protection
|
||||||
|
/// for managed memory accesses (not using host page protection).
|
||||||
|
/// </summary>
|
||||||
|
internal readonly struct ManagedPageFlags
|
||||||
|
{
|
||||||
|
public const int PageBits = 12;
|
||||||
|
public const int PageSize = 1 << PageBits;
|
||||||
|
public const int PageMask = PageSize - 1;
|
||||||
|
|
||||||
|
private readonly ulong[] _pageBitmap;
|
||||||
|
|
||||||
|
public const int PageToPteShift = 5; // 32 pages (2 bits each) in one ulong page table entry.
|
||||||
|
public const ulong BlockMappedMask = 0x5555555555555555; // First bit of each table entry set.
|
||||||
|
|
||||||
|
private enum ManagedPtBits : ulong
|
||||||
|
{
|
||||||
|
Unmapped = 0,
|
||||||
|
Mapped,
|
||||||
|
WriteTracked,
|
||||||
|
ReadWriteTracked,
|
||||||
|
|
||||||
|
MappedReplicated = 0x5555555555555555,
|
||||||
|
WriteTrackedReplicated = 0xaaaaaaaaaaaaaaaa,
|
||||||
|
ReadWriteTrackedReplicated = ulong.MaxValue,
|
||||||
|
}
|
||||||
|
|
||||||
|
public ManagedPageFlags(int addressSpaceBits)
|
||||||
|
{
|
||||||
|
int bits = Math.Max(0, addressSpaceBits - (PageBits + PageToPteShift));
|
||||||
|
_pageBitmap = new ulong[1 << bits];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
private static int GetPagesCount(ulong va, ulong size, out ulong startVa)
|
||||||
|
{
|
||||||
|
// WARNING: Always check if ulong does not overflow during the operations.
|
||||||
|
startVa = va & ~(ulong)PageMask;
|
||||||
|
ulong vaSpan = (va - startVa + size + PageMask) & ~(ulong)PageMask;
|
||||||
|
|
||||||
|
return (int)(vaSpan / PageSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if the page at a given CPU virtual address is mapped.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="va">Virtual address to check</param>
|
||||||
|
/// <returns>True if the address is mapped, false otherwise</returns>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public readonly bool IsMapped(ulong va)
|
||||||
|
{
|
||||||
|
ulong page = va >> PageBits;
|
||||||
|
|
||||||
|
int bit = (int)((page & 31) << 1);
|
||||||
|
|
||||||
|
int pageIndex = (int)(page >> PageToPteShift);
|
||||||
|
ref ulong pageRef = ref _pageBitmap[pageIndex];
|
||||||
|
|
||||||
|
ulong pte = Volatile.Read(ref pageRef);
|
||||||
|
|
||||||
|
return ((pte >> bit) & 3) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
private static void GetPageBlockRange(ulong pageStart, ulong pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex)
|
||||||
|
{
|
||||||
|
startMask = ulong.MaxValue << ((int)(pageStart & 31) << 1);
|
||||||
|
endMask = ulong.MaxValue >> (64 - ((int)(pageEnd & 31) << 1));
|
||||||
|
|
||||||
|
pageIndex = (int)(pageStart >> PageToPteShift);
|
||||||
|
pageEndIndex = (int)((pageEnd - 1) >> PageToPteShift);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
public readonly bool IsRangeMapped(ulong va, ulong size)
|
||||||
|
{
|
||||||
|
int pages = GetPagesCount(va, size, out _);
|
||||||
|
|
||||||
|
if (pages == 1)
|
||||||
|
{
|
||||||
|
return IsMapped(va);
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong pageStart = va >> PageBits;
|
||||||
|
ulong pageEnd = pageStart + (ulong)pages;
|
||||||
|
|
||||||
|
GetPageBlockRange(pageStart, pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex);
|
||||||
|
|
||||||
|
// Check if either bit in each 2 bit page entry is set.
|
||||||
|
// OR the block with itself shifted down by 1, and check the first bit of each entry.
|
||||||
|
|
||||||
|
ulong mask = BlockMappedMask & startMask;
|
||||||
|
|
||||||
|
while (pageIndex <= pageEndIndex)
|
||||||
|
{
|
||||||
|
if (pageIndex == pageEndIndex)
|
||||||
|
{
|
||||||
|
mask &= endMask;
|
||||||
|
}
|
||||||
|
|
||||||
|
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
||||||
|
ulong pte = Volatile.Read(ref pageRef);
|
||||||
|
|
||||||
|
pte |= pte >> 1;
|
||||||
|
if ((pte & mask) != mask)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
mask = BlockMappedMask;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reprotect a region of virtual memory for tracking.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="va">Virtual address base</param>
|
||||||
|
/// <param name="size">Size of the region to protect</param>
|
||||||
|
/// <param name="protection">Memory protection to set</param>
|
||||||
|
public readonly void TrackingReprotect(ulong va, ulong size, MemoryPermission protection)
|
||||||
|
{
|
||||||
|
// Protection is inverted on software pages, since the default value is 0.
|
||||||
|
protection = (~protection) & MemoryPermission.ReadAndWrite;
|
||||||
|
|
||||||
|
int pages = GetPagesCount(va, size, out va);
|
||||||
|
ulong pageStart = va >> PageBits;
|
||||||
|
|
||||||
|
if (pages == 1)
|
||||||
|
{
|
||||||
|
ulong protTag = protection switch
|
||||||
|
{
|
||||||
|
MemoryPermission.None => (ulong)ManagedPtBits.Mapped,
|
||||||
|
MemoryPermission.Write => (ulong)ManagedPtBits.WriteTracked,
|
||||||
|
_ => (ulong)ManagedPtBits.ReadWriteTracked,
|
||||||
|
};
|
||||||
|
|
||||||
|
int bit = (int)((pageStart & 31) << 1);
|
||||||
|
|
||||||
|
ulong tagMask = 3UL << bit;
|
||||||
|
ulong invTagMask = ~tagMask;
|
||||||
|
|
||||||
|
ulong tag = protTag << bit;
|
||||||
|
|
||||||
|
int pageIndex = (int)(pageStart >> PageToPteShift);
|
||||||
|
ref ulong pageRef = ref _pageBitmap[pageIndex];
|
||||||
|
|
||||||
|
ulong pte;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
pte = Volatile.Read(ref pageRef);
|
||||||
|
}
|
||||||
|
while ((pte & tagMask) != 0 && Interlocked.CompareExchange(ref pageRef, (pte & invTagMask) | tag, pte) != pte);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ulong pageEnd = pageStart + (ulong)pages;
|
||||||
|
|
||||||
|
GetPageBlockRange(pageStart, pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex);
|
||||||
|
|
||||||
|
ulong mask = startMask;
|
||||||
|
|
||||||
|
ulong protTag = protection switch
|
||||||
|
{
|
||||||
|
MemoryPermission.None => (ulong)ManagedPtBits.MappedReplicated,
|
||||||
|
MemoryPermission.Write => (ulong)ManagedPtBits.WriteTrackedReplicated,
|
||||||
|
_ => (ulong)ManagedPtBits.ReadWriteTrackedReplicated,
|
||||||
|
};
|
||||||
|
|
||||||
|
while (pageIndex <= pageEndIndex)
|
||||||
|
{
|
||||||
|
if (pageIndex == pageEndIndex)
|
||||||
|
{
|
||||||
|
mask &= endMask;
|
||||||
|
}
|
||||||
|
|
||||||
|
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
||||||
|
|
||||||
|
ulong pte;
|
||||||
|
ulong mappedMask;
|
||||||
|
|
||||||
|
// Change the protection of all 2 bit entries that are mapped.
|
||||||
|
do
|
||||||
|
{
|
||||||
|
pte = Volatile.Read(ref pageRef);
|
||||||
|
|
||||||
|
mappedMask = pte | (pte >> 1);
|
||||||
|
mappedMask |= (mappedMask & BlockMappedMask) << 1;
|
||||||
|
mappedMask &= mask; // Only update mapped pages within the given range.
|
||||||
|
}
|
||||||
|
while (Interlocked.CompareExchange(ref pageRef, (pte & (~mappedMask)) | (protTag & mappedMask), pte) != pte);
|
||||||
|
|
||||||
|
mask = ulong.MaxValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Alerts the memory tracking that a given region has been read from or written to.
|
||||||
|
/// This should be called before read/write is performed.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="tracking">Memory tracking structure to call when pages are protected</param>
|
||||||
|
/// <param name="va">Virtual address of the region</param>
|
||||||
|
/// <param name="size">Size of the region</param>
|
||||||
|
/// <param name="write">True if the region was written, false if read</param>
|
||||||
|
/// <param name="exemptId">Optional ID of the handles that should not be signalled</param>
|
||||||
|
/// <remarks>
|
||||||
|
/// This function also validates that the given range is both valid and mapped, and will throw if it is not.
|
||||||
|
/// </remarks>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public readonly void SignalMemoryTracking(MemoryTracking tracking, ulong va, ulong size, bool write, int? exemptId = null)
|
||||||
|
{
|
||||||
|
// Software table, used for managed memory tracking.
|
||||||
|
|
||||||
|
int pages = GetPagesCount(va, size, out _);
|
||||||
|
ulong pageStart = va >> PageBits;
|
||||||
|
|
||||||
|
if (pages == 1)
|
||||||
|
{
|
||||||
|
ulong tag = (ulong)(write ? ManagedPtBits.WriteTracked : ManagedPtBits.ReadWriteTracked);
|
||||||
|
|
||||||
|
int bit = (int)((pageStart & 31) << 1);
|
||||||
|
|
||||||
|
int pageIndex = (int)(pageStart >> PageToPteShift);
|
||||||
|
ref ulong pageRef = ref _pageBitmap[pageIndex];
|
||||||
|
|
||||||
|
ulong pte = Volatile.Read(ref pageRef);
|
||||||
|
ulong state = ((pte >> bit) & 3);
|
||||||
|
|
||||||
|
if (state >= tag)
|
||||||
|
{
|
||||||
|
tracking.VirtualMemoryEvent(va, size, write, precise: false, exemptId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if (state == 0)
|
||||||
|
{
|
||||||
|
ThrowInvalidMemoryRegionException($"Not mapped: va=0x{va:X16}, size=0x{size:X16}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ulong pageEnd = pageStart + (ulong)pages;
|
||||||
|
|
||||||
|
GetPageBlockRange(pageStart, pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex);
|
||||||
|
|
||||||
|
ulong mask = startMask;
|
||||||
|
|
||||||
|
ulong anyTrackingTag = (ulong)ManagedPtBits.WriteTrackedReplicated;
|
||||||
|
|
||||||
|
while (pageIndex <= pageEndIndex)
|
||||||
|
{
|
||||||
|
if (pageIndex == pageEndIndex)
|
||||||
|
{
|
||||||
|
mask &= endMask;
|
||||||
|
}
|
||||||
|
|
||||||
|
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
||||||
|
|
||||||
|
ulong pte = Volatile.Read(ref pageRef);
|
||||||
|
ulong mappedMask = mask & BlockMappedMask;
|
||||||
|
|
||||||
|
ulong mappedPte = pte | (pte >> 1);
|
||||||
|
if ((mappedPte & mappedMask) != mappedMask)
|
||||||
|
{
|
||||||
|
ThrowInvalidMemoryRegionException($"Not mapped: va=0x{va:X16}, size=0x{size:X16}");
|
||||||
|
}
|
||||||
|
|
||||||
|
pte &= mask;
|
||||||
|
if ((pte & anyTrackingTag) != 0) // Search for any tracking.
|
||||||
|
{
|
||||||
|
// Writes trigger any tracking.
|
||||||
|
// Only trigger tracking from reads if both bits are set on any page.
|
||||||
|
if (write || (pte & (pte >> 1) & BlockMappedMask) != 0)
|
||||||
|
{
|
||||||
|
tracking.VirtualMemoryEvent(va, size, write, precise: false, exemptId);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mask = ulong.MaxValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds the given address mapping to the page table.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="va">Virtual memory address</param>
|
||||||
|
/// <param name="size">Size to be mapped</param>
|
||||||
|
public readonly void AddMapping(ulong va, ulong size)
|
||||||
|
{
|
||||||
|
int pages = GetPagesCount(va, size, out _);
|
||||||
|
ulong pageStart = va >> PageBits;
|
||||||
|
ulong pageEnd = pageStart + (ulong)pages;
|
||||||
|
|
||||||
|
GetPageBlockRange(pageStart, pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex);
|
||||||
|
|
||||||
|
ulong mask = startMask;
|
||||||
|
|
||||||
|
while (pageIndex <= pageEndIndex)
|
||||||
|
{
|
||||||
|
if (pageIndex == pageEndIndex)
|
||||||
|
{
|
||||||
|
mask &= endMask;
|
||||||
|
}
|
||||||
|
|
||||||
|
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
||||||
|
|
||||||
|
ulong pte;
|
||||||
|
ulong mappedMask;
|
||||||
|
|
||||||
|
// Map all 2-bit entries that are unmapped.
|
||||||
|
do
|
||||||
|
{
|
||||||
|
pte = Volatile.Read(ref pageRef);
|
||||||
|
|
||||||
|
mappedMask = pte | (pte >> 1);
|
||||||
|
mappedMask |= (mappedMask & BlockMappedMask) << 1;
|
||||||
|
mappedMask |= ~mask; // Treat everything outside the range as mapped, thus unchanged.
|
||||||
|
}
|
||||||
|
while (Interlocked.CompareExchange(ref pageRef, (pte & mappedMask) | (BlockMappedMask & (~mappedMask)), pte) != pte);
|
||||||
|
|
||||||
|
mask = ulong.MaxValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes the given address mapping from the page table.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="va">Virtual memory address</param>
|
||||||
|
/// <param name="size">Size to be unmapped</param>
|
||||||
|
public readonly void RemoveMapping(ulong va, ulong size)
|
||||||
|
{
|
||||||
|
int pages = GetPagesCount(va, size, out _);
|
||||||
|
ulong pageStart = va >> PageBits;
|
||||||
|
ulong pageEnd = pageStart + (ulong)pages;
|
||||||
|
|
||||||
|
GetPageBlockRange(pageStart, pageEnd, out ulong startMask, out ulong endMask, out int pageIndex, out int pageEndIndex);
|
||||||
|
|
||||||
|
startMask = ~startMask;
|
||||||
|
endMask = ~endMask;
|
||||||
|
|
||||||
|
ulong mask = startMask;
|
||||||
|
|
||||||
|
while (pageIndex <= pageEndIndex)
|
||||||
|
{
|
||||||
|
if (pageIndex == pageEndIndex)
|
||||||
|
{
|
||||||
|
mask |= endMask;
|
||||||
|
}
|
||||||
|
|
||||||
|
ref ulong pageRef = ref _pageBitmap[pageIndex++];
|
||||||
|
ulong pte;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
pte = Volatile.Read(ref pageRef);
|
||||||
|
}
|
||||||
|
while (Interlocked.CompareExchange(ref pageRef, pte & mask, pte) != pte);
|
||||||
|
|
||||||
|
mask = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ThrowInvalidMemoryRegionException(string message) => throw new InvalidMemoryRegionException(message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +1,13 @@
|
||||||
using Ryujinx.Memory;
|
using Ryujinx.Memory;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Numerics;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace Ryujinx.Cpu
|
namespace Ryujinx.Cpu
|
||||||
{
|
{
|
||||||
public abstract class MemoryManagerBase : IRefCounted
|
public abstract class VirtualMemoryManagerRefCountedBase<TVirtual, TPhysical> : VirtualMemoryManagerBase<TVirtual, TPhysical>, IRefCounted
|
||||||
|
where TVirtual : IBinaryInteger<TVirtual>
|
||||||
|
where TPhysical : IBinaryInteger<TPhysical>
|
||||||
{
|
{
|
||||||
private int _referenceCount;
|
private int _referenceCount;
|
||||||
|
|
|
@ -343,12 +343,23 @@ namespace Ryujinx.Graphics.Gpu.Engine.Threed
|
||||||
|
|
||||||
bool unalignedChanged = _currentSpecState.SetHasUnalignedStorageBuffer(_channel.BufferManager.HasUnalignedStorageBuffers);
|
bool unalignedChanged = _currentSpecState.SetHasUnalignedStorageBuffer(_channel.BufferManager.HasUnalignedStorageBuffers);
|
||||||
|
|
||||||
if (!_channel.TextureManager.CommitGraphicsBindings(_shaderSpecState) || unalignedChanged)
|
bool scaleMismatch;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
if (!_channel.TextureManager.CommitGraphicsBindings(_shaderSpecState, out scaleMismatch) || unalignedChanged)
|
||||||
{
|
{
|
||||||
// Shader must be reloaded. _vtgWritesRtLayer should not change.
|
// Shader must be reloaded. _vtgWritesRtLayer should not change.
|
||||||
UpdateShaderState();
|
UpdateShaderState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (scaleMismatch)
|
||||||
|
{
|
||||||
|
// Binding textures changed scale of the bound render targets, correct the render target scale and rebind.
|
||||||
|
UpdateRenderTargetState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (scaleMismatch);
|
||||||
|
|
||||||
_channel.BufferManager.CommitGraphicsBindings(_drawState.DrawIndexed);
|
_channel.BufferManager.CommitGraphicsBindings(_drawState.DrawIndexed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,7 @@ namespace Ryujinx.Graphics.Gpu.Image
|
||||||
{
|
{
|
||||||
private const int MinCountForDeletion = 32;
|
private const int MinCountForDeletion = 32;
|
||||||
private const int MaxCapacity = 2048;
|
private const int MaxCapacity = 2048;
|
||||||
private const ulong MaxTextureSizeCapacity = 512 * 1024 * 1024; // MB;
|
private const ulong MaxTextureSizeCapacity = 1024 * 1024 * 1024; // MB;
|
||||||
|
|
||||||
private readonly LinkedList<Texture> _textures;
|
private readonly LinkedList<Texture> _textures;
|
||||||
private ulong _totalSize;
|
private ulong _totalSize;
|
||||||
|
|
|
@ -69,7 +69,7 @@ namespace Ryujinx.Graphics.Gpu.Image
|
||||||
Address = address;
|
Address = address;
|
||||||
Size = size;
|
Size = size;
|
||||||
|
|
||||||
_memoryTracking = physicalMemory.BeginGranularTracking(address, size, ResourceKind.Pool);
|
_memoryTracking = physicalMemory.BeginGranularTracking(address, size, ResourceKind.Pool, RegionFlags.None);
|
||||||
_memoryTracking.RegisterPreciseAction(address, size, PreciseAction);
|
_memoryTracking.RegisterPreciseAction(address, size, PreciseAction);
|
||||||
_modifiedDelegate = RegionModified;
|
_modifiedDelegate = RegionModified;
|
||||||
}
|
}
|
||||||
|
|
|
@ -360,15 +360,16 @@ namespace Ryujinx.Graphics.Gpu.Image
|
||||||
/// Commits bindings on the graphics pipeline.
|
/// Commits bindings on the graphics pipeline.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="specState">Specialization state for the bound shader</param>
|
/// <param name="specState">Specialization state for the bound shader</param>
|
||||||
|
/// <param name="scaleMismatch">True if there is a scale mismatch in the render targets, indicating they must be re-evaluated</param>
|
||||||
/// <returns>True if all bound textures match the current shader specialization state, false otherwise</returns>
|
/// <returns>True if all bound textures match the current shader specialization state, false otherwise</returns>
|
||||||
public bool CommitGraphicsBindings(ShaderSpecializationState specState)
|
public bool CommitGraphicsBindings(ShaderSpecializationState specState, out bool scaleMismatch)
|
||||||
{
|
{
|
||||||
_texturePoolCache.Tick();
|
_texturePoolCache.Tick();
|
||||||
_samplerPoolCache.Tick();
|
_samplerPoolCache.Tick();
|
||||||
|
|
||||||
bool result = _gpBindingsManager.CommitBindings(specState);
|
bool result = _gpBindingsManager.CommitBindings(specState);
|
||||||
|
|
||||||
UpdateRenderTargets();
|
scaleMismatch = UpdateRenderTargets();
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -426,9 +427,12 @@ namespace Ryujinx.Graphics.Gpu.Image
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Update host framebuffer attachments based on currently bound render target buffers.
|
/// Update host framebuffer attachments based on currently bound render target buffers.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void UpdateRenderTargets()
|
/// <returns>True if there is a scale mismatch in the render targets, indicating they must be re-evaluated</returns>
|
||||||
|
public bool UpdateRenderTargets()
|
||||||
{
|
{
|
||||||
bool anyChanged = false;
|
bool anyChanged = false;
|
||||||
|
float expectedScale = RenderTargetScale;
|
||||||
|
bool scaleMismatch = false;
|
||||||
|
|
||||||
Texture dsTexture = _rtDepthStencil;
|
Texture dsTexture = _rtDepthStencil;
|
||||||
ITexture hostDsTexture = null;
|
ITexture hostDsTexture = null;
|
||||||
|
@ -448,6 +452,11 @@ namespace Ryujinx.Graphics.Gpu.Image
|
||||||
{
|
{
|
||||||
_rtHostDs = hostDsTexture;
|
_rtHostDs = hostDsTexture;
|
||||||
anyChanged = true;
|
anyChanged = true;
|
||||||
|
|
||||||
|
if (dsTexture != null && dsTexture.ScaleFactor != expectedScale)
|
||||||
|
{
|
||||||
|
scaleMismatch = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int index = 0; index < _rtColors.Length; index++)
|
for (int index = 0; index < _rtColors.Length; index++)
|
||||||
|
@ -470,6 +479,11 @@ namespace Ryujinx.Graphics.Gpu.Image
|
||||||
{
|
{
|
||||||
_rtHostColors[index] = hostTexture;
|
_rtHostColors[index] = hostTexture;
|
||||||
anyChanged = true;
|
anyChanged = true;
|
||||||
|
|
||||||
|
if (texture != null && texture.ScaleFactor != expectedScale)
|
||||||
|
{
|
||||||
|
scaleMismatch = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -477,6 +491,8 @@ namespace Ryujinx.Graphics.Gpu.Image
|
||||||
{
|
{
|
||||||
_context.Renderer.Pipeline.SetRenderTargets(_rtHostColors, _rtHostDs);
|
_context.Renderer.Pipeline.SetRenderTargets(_rtHostColors, _rtHostDs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return scaleMismatch;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -128,13 +128,13 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
|
|
||||||
if (_useGranular)
|
if (_useGranular)
|
||||||
{
|
{
|
||||||
_memoryTrackingGranular = physicalMemory.BeginGranularTracking(address, size, ResourceKind.Buffer, baseHandles);
|
_memoryTrackingGranular = physicalMemory.BeginGranularTracking(address, size, ResourceKind.Buffer, RegionFlags.UnalignedAccess, baseHandles);
|
||||||
|
|
||||||
_memoryTrackingGranular.RegisterPreciseAction(address, size, PreciseAction);
|
_memoryTrackingGranular.RegisterPreciseAction(address, size, PreciseAction);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_memoryTracking = physicalMemory.BeginTracking(address, size, ResourceKind.Buffer);
|
_memoryTracking = physicalMemory.BeginTracking(address, size, ResourceKind.Buffer, RegionFlags.UnalignedAccess);
|
||||||
|
|
||||||
if (baseHandles != null)
|
if (baseHandles != null)
|
||||||
{
|
{
|
||||||
|
|
|
@ -368,10 +368,11 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
/// <param name="address">CPU virtual address of the region</param>
|
/// <param name="address">CPU virtual address of the region</param>
|
||||||
/// <param name="size">Size of the region</param>
|
/// <param name="size">Size of the region</param>
|
||||||
/// <param name="kind">Kind of the resource being tracked</param>
|
/// <param name="kind">Kind of the resource being tracked</param>
|
||||||
|
/// <param name="flags">Region flags</param>
|
||||||
/// <returns>The memory tracking handle</returns>
|
/// <returns>The memory tracking handle</returns>
|
||||||
public RegionHandle BeginTracking(ulong address, ulong size, ResourceKind kind)
|
public RegionHandle BeginTracking(ulong address, ulong size, ResourceKind kind, RegionFlags flags = RegionFlags.None)
|
||||||
{
|
{
|
||||||
return _cpuMemory.BeginTracking(address, size, (int)kind);
|
return _cpuMemory.BeginTracking(address, size, (int)kind, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -408,12 +409,19 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
/// <param name="address">CPU virtual address of the region</param>
|
/// <param name="address">CPU virtual address of the region</param>
|
||||||
/// <param name="size">Size of the region</param>
|
/// <param name="size">Size of the region</param>
|
||||||
/// <param name="kind">Kind of the resource being tracked</param>
|
/// <param name="kind">Kind of the resource being tracked</param>
|
||||||
|
/// <param name="flags">Region flags</param>
|
||||||
/// <param name="handles">Handles to inherit state from or reuse</param>
|
/// <param name="handles">Handles to inherit state from or reuse</param>
|
||||||
/// <param name="granularity">Desired granularity of write tracking</param>
|
/// <param name="granularity">Desired granularity of write tracking</param>
|
||||||
/// <returns>The memory tracking handle</returns>
|
/// <returns>The memory tracking handle</returns>
|
||||||
public MultiRegionHandle BeginGranularTracking(ulong address, ulong size, ResourceKind kind, IEnumerable<IRegionHandle> handles = null, ulong granularity = 4096)
|
public MultiRegionHandle BeginGranularTracking(
|
||||||
|
ulong address,
|
||||||
|
ulong size,
|
||||||
|
ResourceKind kind,
|
||||||
|
RegionFlags flags = RegionFlags.None,
|
||||||
|
IEnumerable<IRegionHandle> handles = null,
|
||||||
|
ulong granularity = 4096)
|
||||||
{
|
{
|
||||||
return _cpuMemory.BeginGranularTracking(address, size, handles, granularity, (int)kind);
|
return _cpuMemory.BeginGranularTracking(address, size, handles, granularity, (int)kind, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -173,7 +173,7 @@ namespace Ryujinx.Graphics.Gpu.Memory
|
||||||
|
|
||||||
ShrinkOverlapsBufferIfNeeded();
|
ShrinkOverlapsBufferIfNeeded();
|
||||||
|
|
||||||
// If the the range is not properly aligned for sparse mapping,
|
// If the range is not properly aligned for sparse mapping,
|
||||||
// let's just force it to a single range.
|
// let's just force it to a single range.
|
||||||
// This might cause issues in some applications that uses sparse
|
// This might cause issues in some applications that uses sparse
|
||||||
// mappings.
|
// mappings.
|
||||||
|
|
|
@ -22,7 +22,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
|
||||||
private const ushort FileFormatVersionMajor = 1;
|
private const ushort FileFormatVersionMajor = 1;
|
||||||
private const ushort FileFormatVersionMinor = 2;
|
private const ushort FileFormatVersionMinor = 2;
|
||||||
private const uint FileFormatVersionPacked = ((uint)FileFormatVersionMajor << 16) | FileFormatVersionMinor;
|
private const uint FileFormatVersionPacked = ((uint)FileFormatVersionMajor << 16) | FileFormatVersionMinor;
|
||||||
private const uint CodeGenVersion = 6253;
|
private const uint CodeGenVersion = 6462;
|
||||||
|
|
||||||
private const string SharedTocFileName = "shared.toc";
|
private const string SharedTocFileName = "shared.toc";
|
||||||
private const string SharedDataFileName = "shared.data";
|
private const string SharedDataFileName = "shared.data";
|
||||||
|
|
|
@ -356,6 +356,16 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
|
||||||
|
|
||||||
context.AddGlobalVariable(perVertexInputVariable);
|
context.AddGlobalVariable(perVertexInputVariable);
|
||||||
context.Inputs.Add(new IoDefinition(StorageKind.Input, IoVariable.Position), perVertexInputVariable);
|
context.Inputs.Add(new IoDefinition(StorageKind.Input, IoVariable.Position), perVertexInputVariable);
|
||||||
|
|
||||||
|
if (context.Definitions.Stage == ShaderStage.Geometry &&
|
||||||
|
context.Definitions.GpPassthrough &&
|
||||||
|
context.HostCapabilities.SupportsGeometryShaderPassthrough)
|
||||||
|
{
|
||||||
|
context.MemberDecorate(perVertexInputStructType, 0, Decoration.PassthroughNV);
|
||||||
|
context.MemberDecorate(perVertexInputStructType, 1, Decoration.PassthroughNV);
|
||||||
|
context.MemberDecorate(perVertexInputStructType, 2, Decoration.PassthroughNV);
|
||||||
|
context.MemberDecorate(perVertexInputStructType, 3, Decoration.PassthroughNV);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var perVertexOutputStructType = CreatePerVertexStructType(context);
|
var perVertexOutputStructType = CreatePerVertexStructType(context);
|
||||||
|
|
|
@ -24,17 +24,21 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Operand temp = OperandHelper.Local();
|
||||||
|
|
||||||
for (int index = 0; index < phi.SourcesCount; index++)
|
for (int index = 0; index < phi.SourcesCount; index++)
|
||||||
{
|
{
|
||||||
Operand src = phi.GetSource(index);
|
Operand src = phi.GetSource(index);
|
||||||
|
|
||||||
BasicBlock srcBlock = phi.GetBlock(index);
|
BasicBlock srcBlock = phi.GetBlock(index);
|
||||||
|
|
||||||
Operation copyOp = new(Instruction.Copy, phi.Dest, src);
|
Operation copyOp = new(Instruction.Copy, temp, src);
|
||||||
|
|
||||||
srcBlock.Append(copyOp);
|
srcBlock.Append(copyOp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Operation copyOp2 = new(Instruction.Copy, phi.Dest, temp);
|
||||||
|
|
||||||
|
nextNode = block.Operations.AddAfter(node, copyOp2).Next;
|
||||||
block.Operations.Remove(node);
|
block.Operations.Remove(node);
|
||||||
|
|
||||||
node = nextNode;
|
node = nextNode;
|
||||||
|
|
|
@ -322,7 +322,7 @@ namespace Ryujinx.Graphics.Shader.Translation.Optimizations
|
||||||
Operand lhs = operation.GetSource(0);
|
Operand lhs = operation.GetSource(0);
|
||||||
Operand rhs = operation.GetSource(1);
|
Operand rhs = operation.GetSource(1);
|
||||||
|
|
||||||
// Check LHS of the the main multiplication operation. We expect an input being multiplied by gl_FragCoord.w.
|
// Check LHS of the main multiplication operation. We expect an input being multiplied by gl_FragCoord.w.
|
||||||
if (lhs.AsgOp is not Operation attrMulOp || attrMulOp.Inst != (Instruction.FP32 | Instruction.Multiply))
|
if (lhs.AsgOp is not Operation attrMulOp || attrMulOp.Inst != (Instruction.FP32 | Instruction.Multiply))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -136,7 +136,7 @@ namespace Ryujinx.Graphics.Vulkan
|
||||||
{
|
{
|
||||||
instance.EnumeratePhysicalDevices(out var physicalDevices).ThrowOnError();
|
instance.EnumeratePhysicalDevices(out var physicalDevices).ThrowOnError();
|
||||||
|
|
||||||
// First we try to pick the the user preferred GPU.
|
// First we try to pick the user preferred GPU.
|
||||||
for (int i = 0; i < physicalDevices.Length; i++)
|
for (int i = 0; i < physicalDevices.Length; i++)
|
||||||
{
|
{
|
||||||
if (IsPreferredAndSuitableDevice(api, physicalDevices[i], surface, preferredGpuId))
|
if (IsPreferredAndSuitableDevice(api, physicalDevices[i], surface, preferredGpuId))
|
||||||
|
|
|
@ -781,7 +781,9 @@ namespace Ryujinx.Graphics.Vulkan
|
||||||
{
|
{
|
||||||
PrimitiveTopology.Quads => PrimitiveTopology.Triangles,
|
PrimitiveTopology.Quads => PrimitiveTopology.Triangles,
|
||||||
PrimitiveTopology.QuadStrip => PrimitiveTopology.TriangleStrip,
|
PrimitiveTopology.QuadStrip => PrimitiveTopology.TriangleStrip,
|
||||||
PrimitiveTopology.TriangleFan => Capabilities.PortabilitySubset.HasFlag(PortabilitySubsetFlags.NoTriangleFans) ? PrimitiveTopology.Triangles : topology,
|
PrimitiveTopology.TriangleFan or PrimitiveTopology.Polygon => Capabilities.PortabilitySubset.HasFlag(PortabilitySubsetFlags.NoTriangleFans)
|
||||||
|
? PrimitiveTopology.Triangles
|
||||||
|
: topology,
|
||||||
_ => topology,
|
_ => topology,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -791,7 +793,7 @@ namespace Ryujinx.Graphics.Vulkan
|
||||||
return topology switch
|
return topology switch
|
||||||
{
|
{
|
||||||
PrimitiveTopology.Quads => true,
|
PrimitiveTopology.Quads => true,
|
||||||
PrimitiveTopology.TriangleFan => Capabilities.PortabilitySubset.HasFlag(PortabilitySubsetFlags.NoTriangleFans),
|
PrimitiveTopology.TriangleFan or PrimitiveTopology.Polygon => Capabilities.PortabilitySubset.HasFlag(PortabilitySubsetFlags.NoTriangleFans),
|
||||||
_ => false,
|
_ => false,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ using Gtk;
|
||||||
using Ryujinx.Common;
|
using Ryujinx.Common;
|
||||||
using Ryujinx.Common.Configuration;
|
using Ryujinx.Common.Configuration;
|
||||||
using Ryujinx.Common.Logging;
|
using Ryujinx.Common.Logging;
|
||||||
|
using Ryujinx.Common.Utilities;
|
||||||
using Ryujinx.Graphics.GAL;
|
using Ryujinx.Graphics.GAL;
|
||||||
using Ryujinx.Graphics.GAL.Multithreading;
|
using Ryujinx.Graphics.GAL.Multithreading;
|
||||||
using Ryujinx.Graphics.Gpu;
|
using Ryujinx.Graphics.Gpu;
|
||||||
|
@ -378,8 +379,12 @@ namespace Ryujinx.UI
|
||||||
{
|
{
|
||||||
lock (this)
|
lock (this)
|
||||||
{
|
{
|
||||||
var currentTime = DateTime.Now;
|
string applicationName = Device.Processes.ActiveApplication.Name;
|
||||||
string filename = $"ryujinx_capture_{currentTime.Year}-{currentTime.Month:D2}-{currentTime.Day:D2}_{currentTime.Hour:D2}-{currentTime.Minute:D2}-{currentTime.Second:D2}.png";
|
string sanitizedApplicationName = FileSystemUtils.SanitizeFileName(applicationName);
|
||||||
|
DateTime currentTime = DateTime.Now;
|
||||||
|
|
||||||
|
string filename = $"{sanitizedApplicationName}_{currentTime.Year}-{currentTime.Month:D2}-{currentTime.Day:D2}_{currentTime.Hour:D2}-{currentTime.Minute:D2}-{currentTime.Second:D2}.png";
|
||||||
|
|
||||||
string directory = AppDataManager.Mode switch
|
string directory = AppDataManager.Mode switch
|
||||||
{
|
{
|
||||||
AppDataManager.LaunchMode.Portable or AppDataManager.LaunchMode.Custom => System.IO.Path.Combine(AppDataManager.BaseDirPath, "screenshots"),
|
AppDataManager.LaunchMode.Portable or AppDataManager.LaunchMode.Custom => System.IO.Path.Combine(AppDataManager.BaseDirPath, "screenshots"),
|
||||||
|
|
|
@ -44,10 +44,10 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
|
||||||
private readonly Color _textSelectedColor;
|
private readonly Color _textSelectedColor;
|
||||||
private readonly Color _textOverCursorColor;
|
private readonly Color _textOverCursorColor;
|
||||||
|
|
||||||
private readonly IBrush _panelBrush;
|
private readonly Brush _panelBrush;
|
||||||
private readonly IBrush _disabledBrush;
|
private readonly Brush _disabledBrush;
|
||||||
private readonly IBrush _cursorBrush;
|
private readonly Brush _cursorBrush;
|
||||||
private readonly IBrush _selectionBoxBrush;
|
private readonly Brush _selectionBoxBrush;
|
||||||
|
|
||||||
private readonly Pen _textBoxOutlinePen;
|
private readonly Pen _textBoxOutlinePen;
|
||||||
private readonly Pen _cursorPen;
|
private readonly Pen _cursorPen;
|
||||||
|
@ -97,10 +97,10 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
|
||||||
_cursorBrush = new SolidBrush(_textNormalColor);
|
_cursorBrush = new SolidBrush(_textNormalColor);
|
||||||
_selectionBoxBrush = new SolidBrush(selectionBackgroundColor);
|
_selectionBoxBrush = new SolidBrush(selectionBackgroundColor);
|
||||||
|
|
||||||
_textBoxOutlinePen = new Pen(borderColor, _textBoxOutlineWidth);
|
_textBoxOutlinePen = Pens.Solid(borderColor, _textBoxOutlineWidth);
|
||||||
_cursorPen = new Pen(_textNormalColor, cursorWidth);
|
_cursorPen = Pens.Solid(_textNormalColor, cursorWidth);
|
||||||
_selectionBoxPen = new Pen(selectionBackgroundColor, cursorWidth);
|
_selectionBoxPen = Pens.Solid(selectionBackgroundColor, cursorWidth);
|
||||||
_padPressedPen = new Pen(borderColor, _padPressedPenWidth);
|
_padPressedPen = Pens.Solid(borderColor, _padPressedPenWidth);
|
||||||
|
|
||||||
_inputTextFontSize = 20;
|
_inputTextFontSize = 20;
|
||||||
|
|
||||||
|
@ -178,7 +178,7 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
|
||||||
private static void SetGraphicsOptions(IImageProcessingContext context)
|
private static void SetGraphicsOptions(IImageProcessingContext context)
|
||||||
{
|
{
|
||||||
context.GetGraphicsOptions().Antialias = true;
|
context.GetGraphicsOptions().Antialias = true;
|
||||||
context.GetShapeGraphicsOptions().GraphicsOptions.Antialias = true;
|
context.GetDrawingOptions().GraphicsOptions.Antialias = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DrawImmutableElements()
|
private void DrawImmutableElements()
|
||||||
|
@ -293,31 +293,31 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
|
||||||
}
|
}
|
||||||
private static RectangleF MeasureString(string text, Font font)
|
private static RectangleF MeasureString(string text, Font font)
|
||||||
{
|
{
|
||||||
RendererOptions options = new(font);
|
TextOptions options = new(font);
|
||||||
|
|
||||||
if (text == "")
|
if (text == "")
|
||||||
{
|
{
|
||||||
FontRectangle emptyRectangle = TextMeasurer.Measure(" ", options);
|
FontRectangle emptyRectangle = TextMeasurer.MeasureSize(" ", options);
|
||||||
|
|
||||||
return new RectangleF(0, emptyRectangle.Y, 0, emptyRectangle.Height);
|
return new RectangleF(0, emptyRectangle.Y, 0, emptyRectangle.Height);
|
||||||
}
|
}
|
||||||
|
|
||||||
FontRectangle rectangle = TextMeasurer.Measure(text, options);
|
FontRectangle rectangle = TextMeasurer.MeasureSize(text, options);
|
||||||
|
|
||||||
return new RectangleF(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
|
return new RectangleF(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static RectangleF MeasureString(ReadOnlySpan<char> text, Font font)
|
private static RectangleF MeasureString(ReadOnlySpan<char> text, Font font)
|
||||||
{
|
{
|
||||||
RendererOptions options = new(font);
|
TextOptions options = new(font);
|
||||||
|
|
||||||
if (text == "")
|
if (text == "")
|
||||||
{
|
{
|
||||||
FontRectangle emptyRectangle = TextMeasurer.Measure(" ", options);
|
FontRectangle emptyRectangle = TextMeasurer.MeasureSize(" ", options);
|
||||||
return new RectangleF(0, emptyRectangle.Y, 0, emptyRectangle.Height);
|
return new RectangleF(0, emptyRectangle.Y, 0, emptyRectangle.Height);
|
||||||
}
|
}
|
||||||
|
|
||||||
FontRectangle rectangle = TextMeasurer.Measure(text, options);
|
FontRectangle rectangle = TextMeasurer.MeasureSize(text, options);
|
||||||
|
|
||||||
return new RectangleF(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
|
return new RectangleF(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
|
||||||
}
|
}
|
||||||
|
@ -350,7 +350,7 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
|
||||||
// Draw the cursor on top of the text and redraw the text with a different color if necessary.
|
// Draw the cursor on top of the text and redraw the text with a different color if necessary.
|
||||||
|
|
||||||
Color cursorTextColor;
|
Color cursorTextColor;
|
||||||
IBrush cursorBrush;
|
Brush cursorBrush;
|
||||||
Pen cursorPen;
|
Pen cursorPen;
|
||||||
|
|
||||||
float cursorPositionYTop = inputTextY + 1;
|
float cursorPositionYTop = inputTextY + 1;
|
||||||
|
@ -435,7 +435,7 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
|
||||||
new PointF(cursorPositionXLeft, cursorPositionYBottom),
|
new PointF(cursorPositionXLeft, cursorPositionYBottom),
|
||||||
};
|
};
|
||||||
|
|
||||||
context.DrawLines(cursorPen, points);
|
context.DrawLine(cursorPen, points);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -466,7 +466,7 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
|
||||||
|
|
||||||
private void DrawPadButton(IImageProcessingContext context, PointF point, Image icon, string label, bool pressed, bool enabled)
|
private void DrawPadButton(IImageProcessingContext context, PointF point, Image icon, string label, bool pressed, bool enabled)
|
||||||
{
|
{
|
||||||
// Use relative positions so we can center the the entire drawing later.
|
// Use relative positions so we can center the entire drawing later.
|
||||||
|
|
||||||
float iconX = 0;
|
float iconX = 0;
|
||||||
float iconY = 0;
|
float iconY = 0;
|
||||||
|
@ -522,7 +522,7 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
|
||||||
{
|
{
|
||||||
var labelRectangle = MeasureString(ControllerToggleText, _labelsTextFont);
|
var labelRectangle = MeasureString(ControllerToggleText, _labelsTextFont);
|
||||||
|
|
||||||
// Use relative positions so we can center the the entire drawing later.
|
// Use relative positions so we can center the entire drawing later.
|
||||||
|
|
||||||
float keyWidth = _keyModeIcon.Width;
|
float keyWidth = _keyModeIcon.Width;
|
||||||
float keyHeight = _keyModeIcon.Height;
|
float keyHeight = _keyModeIcon.Height;
|
||||||
|
@ -562,12 +562,12 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard
|
||||||
|
|
||||||
// Convert the pixel format used in the image to the one used in the Switch surface.
|
// Convert the pixel format used in the image to the one used in the Switch surface.
|
||||||
|
|
||||||
if (!_surface.TryGetSinglePixelSpan(out Span<Argb32> pixels))
|
if (!_surface.DangerousTryGetSinglePixelMemory(out Memory<Argb32> pixels))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_bufferData = MemoryMarshal.AsBytes(pixels).ToArray();
|
_bufferData = MemoryMarshal.AsBytes(pixels.Span).ToArray();
|
||||||
Span<uint> dataConvert = MemoryMarshal.Cast<byte, uint>(_bufferData);
|
Span<uint> dataConvert = MemoryMarshal.Cast<byte, uint>(_bufferData);
|
||||||
|
|
||||||
Debug.Assert(_bufferData.Length == _surfaceInfo.Size);
|
Debug.Assert(_bufferData.Length == _surfaceInfo.Size);
|
||||||
|
|
|
@ -11,12 +11,8 @@ namespace Ryujinx.Memory
|
||||||
/// Represents a address space manager.
|
/// Represents a address space manager.
|
||||||
/// Supports virtual memory region mapping, address translation and read/write access to mapped regions.
|
/// Supports virtual memory region mapping, address translation and read/write access to mapped regions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class AddressSpaceManager : IVirtualMemoryManager, IWritableBlock
|
public sealed class AddressSpaceManager : VirtualMemoryManagerBase<ulong, nuint>, IVirtualMemoryManager, IWritableBlock
|
||||||
{
|
{
|
||||||
public const int PageBits = PageTable<nuint>.PageBits;
|
|
||||||
public const int PageSize = PageTable<nuint>.PageSize;
|
|
||||||
public const int PageMask = PageTable<nuint>.PageMask;
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public bool Supports4KBPages => true;
|
public bool Supports4KBPages => true;
|
||||||
|
|
||||||
|
@ -25,11 +21,11 @@ namespace Ryujinx.Memory
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int AddressSpaceBits { get; }
|
public int AddressSpaceBits { get; }
|
||||||
|
|
||||||
private readonly ulong _addressSpaceSize;
|
|
||||||
|
|
||||||
private readonly MemoryBlock _backingMemory;
|
private readonly MemoryBlock _backingMemory;
|
||||||
private readonly PageTable<nuint> _pageTable;
|
private readonly PageTable<nuint> _pageTable;
|
||||||
|
|
||||||
|
protected override ulong AddressSpaceSize { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of the memory manager.
|
/// Creates a new instance of the memory manager.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -47,7 +43,7 @@ namespace Ryujinx.Memory
|
||||||
}
|
}
|
||||||
|
|
||||||
AddressSpaceBits = asBits;
|
AddressSpaceBits = asBits;
|
||||||
_addressSpaceSize = asSize;
|
AddressSpaceSize = asSize;
|
||||||
_backingMemory = backingMemory;
|
_backingMemory = backingMemory;
|
||||||
_pageTable = new PageTable<nuint>();
|
_pageTable = new PageTable<nuint>();
|
||||||
}
|
}
|
||||||
|
@ -102,12 +98,6 @@ namespace Ryujinx.Memory
|
||||||
return MemoryMarshal.Cast<byte, T>(GetSpan(va, Unsafe.SizeOf<T>()))[0];
|
return MemoryMarshal.Cast<byte, T>(GetSpan(va, Unsafe.SizeOf<T>()))[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public void Read(ulong va, Span<byte> data)
|
|
||||||
{
|
|
||||||
ReadImpl(va, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public void Write<T>(ulong va, T value) where T : unmanaged
|
public void Write<T>(ulong va, T value) where T : unmanaged
|
||||||
{
|
{
|
||||||
|
@ -174,7 +164,7 @@ namespace Ryujinx.Memory
|
||||||
{
|
{
|
||||||
Span<byte> data = new byte[size];
|
Span<byte> data = new byte[size];
|
||||||
|
|
||||||
ReadImpl(va, data);
|
Read(va, data);
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
@ -346,34 +336,6 @@ namespace Ryujinx.Memory
|
||||||
return regions;
|
return regions;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ReadImpl(ulong va, Span<byte> data)
|
|
||||||
{
|
|
||||||
if (data.Length == 0)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
AssertValidAddressAndSize(va, (ulong)data.Length);
|
|
||||||
|
|
||||||
int offset = 0, size;
|
|
||||||
|
|
||||||
if ((va & PageMask) != 0)
|
|
||||||
{
|
|
||||||
size = Math.Min(data.Length, PageSize - (int)(va & PageMask));
|
|
||||||
|
|
||||||
GetHostSpanContiguous(va, size).CopyTo(data[..size]);
|
|
||||||
|
|
||||||
offset += size;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (; offset < data.Length; offset += size)
|
|
||||||
{
|
|
||||||
size = Math.Min(data.Length - offset, PageSize);
|
|
||||||
|
|
||||||
GetHostSpanContiguous(va + (ulong)offset, size).CopyTo(data.Slice(offset, size));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public bool IsMapped(ulong va)
|
public bool IsMapped(ulong va)
|
||||||
|
@ -414,37 +376,6 @@ namespace Ryujinx.Memory
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool ValidateAddress(ulong va)
|
|
||||||
{
|
|
||||||
return va < _addressSpaceSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks if the combination of virtual address and size is part of the addressable space.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="va">Virtual address of the range</param>
|
|
||||||
/// <param name="size">Size of the range in bytes</param>
|
|
||||||
/// <returns>True if the combination of virtual address and size is part of the addressable space</returns>
|
|
||||||
private bool ValidateAddressAndSize(ulong va, ulong size)
|
|
||||||
{
|
|
||||||
ulong endVa = va + size;
|
|
||||||
return endVa >= va && endVa >= size && endVa <= _addressSpaceSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Ensures the combination of virtual address and size is part of the addressable space.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="va">Virtual address of the range</param>
|
|
||||||
/// <param name="size">Size of the range in bytes</param>
|
|
||||||
/// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified outside the addressable space</exception>
|
|
||||||
private void AssertValidAddressAndSize(ulong va, ulong size)
|
|
||||||
{
|
|
||||||
if (!ValidateAddressAndSize(va, size))
|
|
||||||
{
|
|
||||||
throw new InvalidMemoryRegionException($"va=0x{va:X16}, size=0x{size:X16}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private unsafe Span<byte> GetHostSpanContiguous(ulong va, int size)
|
private unsafe Span<byte> GetHostSpanContiguous(ulong va, int size)
|
||||||
{
|
{
|
||||||
return new Span<byte>((void*)GetHostAddress(va), size);
|
return new Span<byte>((void*)GetHostAddress(va), size);
|
||||||
|
@ -461,7 +392,7 @@ namespace Ryujinx.Memory
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public void TrackingReprotect(ulong va, ulong size, MemoryPermission protection)
|
public void TrackingReprotect(ulong va, ulong size, MemoryPermission protection, bool guest = false)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
@ -471,5 +402,11 @@ namespace Ryujinx.Memory
|
||||||
{
|
{
|
||||||
// Only the ARM Memory Manager has tracking for now.
|
// Only the ARM Memory Manager has tracking for now.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override unsafe Span<byte> GetPhysicalAddressSpan(nuint pa, int size)
|
||||||
|
=> new((void*)pa, size);
|
||||||
|
|
||||||
|
protected override nuint TranslateVirtualAddressForRead(ulong va)
|
||||||
|
=> GetHostAddress(va);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -214,6 +214,7 @@ namespace Ryujinx.Memory
|
||||||
/// <param name="va">Virtual address base</param>
|
/// <param name="va">Virtual address base</param>
|
||||||
/// <param name="size">Size of the region to protect</param>
|
/// <param name="size">Size of the region to protect</param>
|
||||||
/// <param name="protection">Memory protection to set</param>
|
/// <param name="protection">Memory protection to set</param>
|
||||||
void TrackingReprotect(ulong va, ulong size, MemoryPermission protection);
|
/// <param name="guest">True if the protection is for guest access, false otherwise</param>
|
||||||
|
void TrackingReprotect(ulong va, ulong size, MemoryPermission protection, bool guest);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -174,7 +174,7 @@ namespace Ryujinx.Memory
|
||||||
/// <param name="offset">Starting offset of the range being read</param>
|
/// <param name="offset">Starting offset of the range being read</param>
|
||||||
/// <param name="data">Span where the bytes being read will be copied to</param>
|
/// <param name="data">Span where the bytes being read will be copied to</param>
|
||||||
/// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
|
/// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
|
||||||
/// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified for the the data is out of range</exception>
|
/// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified for the data is out of range</exception>
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void Read(ulong offset, Span<byte> data)
|
public void Read(ulong offset, Span<byte> data)
|
||||||
{
|
{
|
||||||
|
@ -188,7 +188,7 @@ namespace Ryujinx.Memory
|
||||||
/// <param name="offset">Offset where the data is located</param>
|
/// <param name="offset">Offset where the data is located</param>
|
||||||
/// <returns>Data at the specified address</returns>
|
/// <returns>Data at the specified address</returns>
|
||||||
/// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
|
/// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
|
||||||
/// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified for the the data is out of range</exception>
|
/// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified for the data is out of range</exception>
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public T Read<T>(ulong offset) where T : unmanaged
|
public T Read<T>(ulong offset) where T : unmanaged
|
||||||
{
|
{
|
||||||
|
@ -201,7 +201,7 @@ namespace Ryujinx.Memory
|
||||||
/// <param name="offset">Starting offset of the range being written</param>
|
/// <param name="offset">Starting offset of the range being written</param>
|
||||||
/// <param name="data">Span where the bytes being written will be copied from</param>
|
/// <param name="data">Span where the bytes being written will be copied from</param>
|
||||||
/// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
|
/// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
|
||||||
/// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified for the the data is out of range</exception>
|
/// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified for the data is out of range</exception>
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void Write(ulong offset, ReadOnlySpan<byte> data)
|
public void Write(ulong offset, ReadOnlySpan<byte> data)
|
||||||
{
|
{
|
||||||
|
@ -215,7 +215,7 @@ namespace Ryujinx.Memory
|
||||||
/// <param name="offset">Offset to write the data into</param>
|
/// <param name="offset">Offset to write the data into</param>
|
||||||
/// <param name="data">Data to be written</param>
|
/// <param name="data">Data to be written</param>
|
||||||
/// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
|
/// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
|
||||||
/// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified for the the data is out of range</exception>
|
/// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified for the data is out of range</exception>
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void Write<T>(ulong offset, T data) where T : unmanaged
|
public void Write<T>(ulong offset, T data) where T : unmanaged
|
||||||
{
|
{
|
||||||
|
|
|
@ -14,9 +14,14 @@ namespace Ryujinx.Memory.Tracking
|
||||||
|
|
||||||
// Only use these from within the lock.
|
// Only use these from within the lock.
|
||||||
private readonly NonOverlappingRangeList<VirtualRegion> _virtualRegions;
|
private readonly NonOverlappingRangeList<VirtualRegion> _virtualRegions;
|
||||||
|
// Guest virtual regions are a subset of the normal virtual regions, with potentially different protection
|
||||||
|
// and expanded area of effect on platforms that don't support misaligned page protection.
|
||||||
|
private readonly NonOverlappingRangeList<VirtualRegion> _guestVirtualRegions;
|
||||||
|
|
||||||
private readonly int _pageSize;
|
private readonly int _pageSize;
|
||||||
|
|
||||||
|
private readonly bool _singleByteGuestTracking;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This lock must be obtained when traversing or updating the region-handle hierarchy.
|
/// This lock must be obtained when traversing or updating the region-handle hierarchy.
|
||||||
/// It is not required when reading dirty flags.
|
/// It is not required when reading dirty flags.
|
||||||
|
@ -27,16 +32,27 @@ namespace Ryujinx.Memory.Tracking
|
||||||
/// Create a new tracking structure for the given "physical" memory block,
|
/// Create a new tracking structure for the given "physical" memory block,
|
||||||
/// with a given "virtual" memory manager that will provide mappings and virtual memory protection.
|
/// with a given "virtual" memory manager that will provide mappings and virtual memory protection.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// If <paramref name="singleByteGuestTracking" /> is true, the memory manager must also support protection on partially
|
||||||
|
/// unmapped regions without throwing exceptions or dropping protection on the mapped portion.
|
||||||
|
/// </remarks>
|
||||||
/// <param name="memoryManager">Virtual memory manager</param>
|
/// <param name="memoryManager">Virtual memory manager</param>
|
||||||
/// <param name="block">Physical memory block</param>
|
|
||||||
/// <param name="pageSize">Page size of the virtual memory space</param>
|
/// <param name="pageSize">Page size of the virtual memory space</param>
|
||||||
public MemoryTracking(IVirtualMemoryManager memoryManager, int pageSize, InvalidAccessHandler invalidAccessHandler = null)
|
/// <param name="invalidAccessHandler">Method to call for invalid memory accesses</param>
|
||||||
|
/// <param name="singleByteGuestTracking">True if the guest only signals writes for the first byte</param>
|
||||||
|
public MemoryTracking(
|
||||||
|
IVirtualMemoryManager memoryManager,
|
||||||
|
int pageSize,
|
||||||
|
InvalidAccessHandler invalidAccessHandler = null,
|
||||||
|
bool singleByteGuestTracking = false)
|
||||||
{
|
{
|
||||||
_memoryManager = memoryManager;
|
_memoryManager = memoryManager;
|
||||||
_pageSize = pageSize;
|
_pageSize = pageSize;
|
||||||
_invalidAccessHandler = invalidAccessHandler;
|
_invalidAccessHandler = invalidAccessHandler;
|
||||||
|
_singleByteGuestTracking = singleByteGuestTracking;
|
||||||
|
|
||||||
_virtualRegions = new NonOverlappingRangeList<VirtualRegion>();
|
_virtualRegions = new NonOverlappingRangeList<VirtualRegion>();
|
||||||
|
_guestVirtualRegions = new NonOverlappingRangeList<VirtualRegion>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private (ulong address, ulong size) PageAlign(ulong address, ulong size)
|
private (ulong address, ulong size) PageAlign(ulong address, ulong size)
|
||||||
|
@ -62,7 +78,11 @@ namespace Ryujinx.Memory.Tracking
|
||||||
{
|
{
|
||||||
ref var overlaps = ref ThreadStaticArray<VirtualRegion>.Get();
|
ref var overlaps = ref ThreadStaticArray<VirtualRegion>.Get();
|
||||||
|
|
||||||
int count = _virtualRegions.FindOverlapsNonOverlapping(va, size, ref overlaps);
|
for (int type = 0; type < 2; type++)
|
||||||
|
{
|
||||||
|
NonOverlappingRangeList<VirtualRegion> regions = type == 0 ? _virtualRegions : _guestVirtualRegions;
|
||||||
|
|
||||||
|
int count = regions.FindOverlapsNonOverlapping(va, size, ref overlaps);
|
||||||
|
|
||||||
for (int i = 0; i < count; i++)
|
for (int i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
|
@ -79,6 +99,7 @@ namespace Ryujinx.Memory.Tracking
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Indicate that a virtual region has been unmapped.
|
/// Indicate that a virtual region has been unmapped.
|
||||||
|
@ -95,7 +116,11 @@ namespace Ryujinx.Memory.Tracking
|
||||||
{
|
{
|
||||||
ref var overlaps = ref ThreadStaticArray<VirtualRegion>.Get();
|
ref var overlaps = ref ThreadStaticArray<VirtualRegion>.Get();
|
||||||
|
|
||||||
int count = _virtualRegions.FindOverlapsNonOverlapping(va, size, ref overlaps);
|
for (int type = 0; type < 2; type++)
|
||||||
|
{
|
||||||
|
NonOverlappingRangeList<VirtualRegion> regions = type == 0 ? _virtualRegions : _guestVirtualRegions;
|
||||||
|
|
||||||
|
int count = regions.FindOverlapsNonOverlapping(va, size, ref overlaps);
|
||||||
|
|
||||||
for (int i = 0; i < count; i++)
|
for (int i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
|
@ -105,17 +130,44 @@ namespace Ryujinx.Memory.Tracking
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Alter a tracked memory region to properly capture unaligned accesses.
|
||||||
|
/// For most memory manager modes, this does nothing.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="address">Original region address</param>
|
||||||
|
/// <param name="size">Original region size</param>
|
||||||
|
/// <returns>A new address and size for tracking unaligned accesses</returns>
|
||||||
|
internal (ulong newAddress, ulong newSize) GetUnalignedSafeRegion(ulong address, ulong size)
|
||||||
|
{
|
||||||
|
if (_singleByteGuestTracking)
|
||||||
|
{
|
||||||
|
// The guest only signals the first byte of each memory access with the current memory manager.
|
||||||
|
// To catch unaligned access properly, we need to also protect the page before the address.
|
||||||
|
|
||||||
|
// Assume that the address and size are already aligned.
|
||||||
|
|
||||||
|
return (address - (ulong)_pageSize, size + (ulong)_pageSize);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return (address, size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get a list of virtual regions that a handle covers.
|
/// Get a list of virtual regions that a handle covers.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="va">Starting virtual memory address of the handle</param>
|
/// <param name="va">Starting virtual memory address of the handle</param>
|
||||||
/// <param name="size">Size of the handle's memory region</param>
|
/// <param name="size">Size of the handle's memory region</param>
|
||||||
|
/// <param name="guest">True if getting handles for guest protection, false otherwise</param>
|
||||||
/// <returns>A list of virtual regions within the given range</returns>
|
/// <returns>A list of virtual regions within the given range</returns>
|
||||||
internal List<VirtualRegion> GetVirtualRegionsForHandle(ulong va, ulong size)
|
internal List<VirtualRegion> GetVirtualRegionsForHandle(ulong va, ulong size, bool guest)
|
||||||
{
|
{
|
||||||
List<VirtualRegion> result = new();
|
List<VirtualRegion> result = new();
|
||||||
_virtualRegions.GetOrAddRegions(result, va, size, (va, size) => new VirtualRegion(this, va, size));
|
NonOverlappingRangeList<VirtualRegion> regions = guest ? _guestVirtualRegions : _virtualRegions;
|
||||||
|
regions.GetOrAddRegions(result, va, size, (va, size) => new VirtualRegion(this, va, size, guest));
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -125,9 +177,16 @@ namespace Ryujinx.Memory.Tracking
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="region">Region to remove</param>
|
/// <param name="region">Region to remove</param>
|
||||||
internal void RemoveVirtual(VirtualRegion region)
|
internal void RemoveVirtual(VirtualRegion region)
|
||||||
|
{
|
||||||
|
if (region.Guest)
|
||||||
|
{
|
||||||
|
_guestVirtualRegions.Remove(region);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
_virtualRegions.Remove(region);
|
_virtualRegions.Remove(region);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Obtains a memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
|
/// Obtains a memory tracking handle for the given virtual region, with a specified granularity. This should be disposed when finished with.
|
||||||
|
@ -137,10 +196,11 @@ namespace Ryujinx.Memory.Tracking
|
||||||
/// <param name="handles">Handles to inherit state from or reuse. When none are present, provide null</param>
|
/// <param name="handles">Handles to inherit state from or reuse. When none are present, provide null</param>
|
||||||
/// <param name="granularity">Desired granularity of write tracking</param>
|
/// <param name="granularity">Desired granularity of write tracking</param>
|
||||||
/// <param name="id">Handle ID</param>
|
/// <param name="id">Handle ID</param>
|
||||||
|
/// <param name="flags">Region flags</param>
|
||||||
/// <returns>The memory tracking handle</returns>
|
/// <returns>The memory tracking handle</returns>
|
||||||
public MultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity, int id)
|
public MultiRegionHandle BeginGranularTracking(ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity, int id, RegionFlags flags = RegionFlags.None)
|
||||||
{
|
{
|
||||||
return new MultiRegionHandle(this, address, size, handles, granularity, id);
|
return new MultiRegionHandle(this, address, size, handles, granularity, id, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -164,15 +224,16 @@ namespace Ryujinx.Memory.Tracking
|
||||||
/// <param name="address">CPU virtual address of the region</param>
|
/// <param name="address">CPU virtual address of the region</param>
|
||||||
/// <param name="size">Size of the region</param>
|
/// <param name="size">Size of the region</param>
|
||||||
/// <param name="id">Handle ID</param>
|
/// <param name="id">Handle ID</param>
|
||||||
|
/// <param name="flags">Region flags</param>
|
||||||
/// <returns>The memory tracking handle</returns>
|
/// <returns>The memory tracking handle</returns>
|
||||||
public RegionHandle BeginTracking(ulong address, ulong size, int id)
|
public RegionHandle BeginTracking(ulong address, ulong size, int id, RegionFlags flags = RegionFlags.None)
|
||||||
{
|
{
|
||||||
var (paAddress, paSize) = PageAlign(address, size);
|
var (paAddress, paSize) = PageAlign(address, size);
|
||||||
|
|
||||||
lock (TrackingLock)
|
lock (TrackingLock)
|
||||||
{
|
{
|
||||||
bool mapped = _memoryManager.IsRangeMapped(address, size);
|
bool mapped = _memoryManager.IsRangeMapped(address, size);
|
||||||
RegionHandle handle = new(this, paAddress, paSize, address, size, id, mapped);
|
RegionHandle handle = new(this, paAddress, paSize, address, size, id, flags, mapped);
|
||||||
|
|
||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
|
@ -186,15 +247,16 @@ namespace Ryujinx.Memory.Tracking
|
||||||
/// <param name="bitmap">The bitmap owning the dirty flag for this handle</param>
|
/// <param name="bitmap">The bitmap owning the dirty flag for this handle</param>
|
||||||
/// <param name="bit">The bit of this handle within the dirty flag</param>
|
/// <param name="bit">The bit of this handle within the dirty flag</param>
|
||||||
/// <param name="id">Handle ID</param>
|
/// <param name="id">Handle ID</param>
|
||||||
|
/// <param name="flags">Region flags</param>
|
||||||
/// <returns>The memory tracking handle</returns>
|
/// <returns>The memory tracking handle</returns>
|
||||||
internal RegionHandle BeginTrackingBitmap(ulong address, ulong size, ConcurrentBitmap bitmap, int bit, int id)
|
internal RegionHandle BeginTrackingBitmap(ulong address, ulong size, ConcurrentBitmap bitmap, int bit, int id, RegionFlags flags = RegionFlags.None)
|
||||||
{
|
{
|
||||||
var (paAddress, paSize) = PageAlign(address, size);
|
var (paAddress, paSize) = PageAlign(address, size);
|
||||||
|
|
||||||
lock (TrackingLock)
|
lock (TrackingLock)
|
||||||
{
|
{
|
||||||
bool mapped = _memoryManager.IsRangeMapped(address, size);
|
bool mapped = _memoryManager.IsRangeMapped(address, size);
|
||||||
RegionHandle handle = new(this, paAddress, paSize, address, size, bitmap, bit, id, mapped);
|
RegionHandle handle = new(this, paAddress, paSize, address, size, bitmap, bit, id, flags, mapped);
|
||||||
|
|
||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
|
@ -202,6 +264,7 @@ namespace Ryujinx.Memory.Tracking
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Signal that a virtual memory event happened at the given location.
|
/// Signal that a virtual memory event happened at the given location.
|
||||||
|
/// The memory event is assumed to be triggered by guest code.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="address">Virtual address accessed</param>
|
/// <param name="address">Virtual address accessed</param>
|
||||||
/// <param name="size">Size of the region affected in bytes</param>
|
/// <param name="size">Size of the region affected in bytes</param>
|
||||||
|
@ -209,7 +272,7 @@ namespace Ryujinx.Memory.Tracking
|
||||||
/// <returns>True if the event triggered any tracking regions, false otherwise</returns>
|
/// <returns>True if the event triggered any tracking regions, false otherwise</returns>
|
||||||
public bool VirtualMemoryEvent(ulong address, ulong size, bool write)
|
public bool VirtualMemoryEvent(ulong address, ulong size, bool write)
|
||||||
{
|
{
|
||||||
return VirtualMemoryEvent(address, size, write, precise: false, null);
|
return VirtualMemoryEvent(address, size, write, precise: false, exemptId: null, guest: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -222,8 +285,9 @@ namespace Ryujinx.Memory.Tracking
|
||||||
/// <param name="write">Whether the region was written to or read</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>
|
/// <param name="precise">True if the access is precise, false otherwise</param>
|
||||||
/// <param name="exemptId">Optional ID that of the handles that should not be signalled</param>
|
/// <param name="exemptId">Optional ID that of the handles that should not be signalled</param>
|
||||||
|
/// <param name="guest">True if the access is from the guest, false otherwise</param>
|
||||||
/// <returns>True if the event triggered any tracking regions, false otherwise</returns>
|
/// <returns>True if the event triggered any tracking regions, false otherwise</returns>
|
||||||
public bool VirtualMemoryEvent(ulong address, ulong size, bool write, bool precise, int? exemptId = null)
|
public bool VirtualMemoryEvent(ulong address, ulong size, bool write, bool precise, int? exemptId = null, bool guest = false)
|
||||||
{
|
{
|
||||||
// Look up the virtual region using the region list.
|
// Look up the virtual region using the region list.
|
||||||
// Signal up the chain to relevant handles.
|
// Signal up the chain to relevant handles.
|
||||||
|
@ -234,7 +298,9 @@ namespace Ryujinx.Memory.Tracking
|
||||||
{
|
{
|
||||||
ref var overlaps = ref ThreadStaticArray<VirtualRegion>.Get();
|
ref var overlaps = ref ThreadStaticArray<VirtualRegion>.Get();
|
||||||
|
|
||||||
int count = _virtualRegions.FindOverlapsNonOverlapping(address, size, ref overlaps);
|
NonOverlappingRangeList<VirtualRegion> regions = guest ? _guestVirtualRegions : _virtualRegions;
|
||||||
|
|
||||||
|
int count = regions.FindOverlapsNonOverlapping(address, size, ref overlaps);
|
||||||
|
|
||||||
if (count == 0 && !precise)
|
if (count == 0 && !precise)
|
||||||
{
|
{
|
||||||
|
@ -242,7 +308,7 @@ namespace Ryujinx.Memory.Tracking
|
||||||
{
|
{
|
||||||
// TODO: There is currently the possibility that a page can be protected after its virtual region is removed.
|
// TODO: There is currently the possibility that a page can be protected after its virtual region is removed.
|
||||||
// This code handles that case when it happens, but it would be better to find out how this happens.
|
// This code handles that case when it happens, but it would be better to find out how this happens.
|
||||||
_memoryManager.TrackingReprotect(address & ~(ulong)(_pageSize - 1), (ulong)_pageSize, MemoryPermission.ReadAndWrite);
|
_memoryManager.TrackingReprotect(address & ~(ulong)(_pageSize - 1), (ulong)_pageSize, MemoryPermission.ReadAndWrite, guest);
|
||||||
return true; // This memory _should_ be mapped, so we need to try again.
|
return true; // This memory _should_ be mapped, so we need to try again.
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -252,6 +318,12 @@ namespace Ryujinx.Memory.Tracking
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
if (guest && _singleByteGuestTracking)
|
||||||
|
{
|
||||||
|
// Increase the access size to trigger handles with misaligned accesses.
|
||||||
|
size += (ulong)_pageSize;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < count; i++)
|
for (int i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
VirtualRegion region = overlaps[i];
|
VirtualRegion region = overlaps[i];
|
||||||
|
@ -285,9 +357,10 @@ namespace Ryujinx.Memory.Tracking
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="region">Region to reprotect</param>
|
/// <param name="region">Region to reprotect</param>
|
||||||
/// <param name="permission">Memory permission to protect with</param>
|
/// <param name="permission">Memory permission to protect with</param>
|
||||||
internal void ProtectVirtualRegion(VirtualRegion region, MemoryPermission permission)
|
/// <param name="guest">True if the protection is for guest access, false otherwise</param>
|
||||||
|
internal void ProtectVirtualRegion(VirtualRegion region, MemoryPermission permission, bool guest)
|
||||||
{
|
{
|
||||||
_memoryManager.TrackingReprotect(region.Address, region.Size, permission);
|
_memoryManager.TrackingReprotect(region.Address, region.Size, permission, guest);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -37,7 +37,8 @@ namespace Ryujinx.Memory.Tracking
|
||||||
ulong size,
|
ulong size,
|
||||||
IEnumerable<IRegionHandle> handles,
|
IEnumerable<IRegionHandle> handles,
|
||||||
ulong granularity,
|
ulong granularity,
|
||||||
int id)
|
int id,
|
||||||
|
RegionFlags flags)
|
||||||
{
|
{
|
||||||
_handles = new RegionHandle[(size + granularity - 1) / granularity];
|
_handles = new RegionHandle[(size + granularity - 1) / granularity];
|
||||||
Granularity = granularity;
|
Granularity = granularity;
|
||||||
|
@ -62,7 +63,7 @@ namespace Ryujinx.Memory.Tracking
|
||||||
// Fill any gap left before this handle.
|
// Fill any gap left before this handle.
|
||||||
while (i < startIndex)
|
while (i < startIndex)
|
||||||
{
|
{
|
||||||
RegionHandle fillHandle = tracking.BeginTrackingBitmap(address + (ulong)i * granularity, granularity, _dirtyBitmap, i, id);
|
RegionHandle fillHandle = tracking.BeginTrackingBitmap(address + (ulong)i * granularity, granularity, _dirtyBitmap, i, id, flags);
|
||||||
fillHandle.Parent = this;
|
fillHandle.Parent = this;
|
||||||
_handles[i++] = fillHandle;
|
_handles[i++] = fillHandle;
|
||||||
}
|
}
|
||||||
|
@ -83,7 +84,7 @@ namespace Ryujinx.Memory.Tracking
|
||||||
|
|
||||||
while (i < endIndex)
|
while (i < endIndex)
|
||||||
{
|
{
|
||||||
RegionHandle splitHandle = tracking.BeginTrackingBitmap(address + (ulong)i * granularity, granularity, _dirtyBitmap, i, id);
|
RegionHandle splitHandle = tracking.BeginTrackingBitmap(address + (ulong)i * granularity, granularity, _dirtyBitmap, i, id, flags);
|
||||||
splitHandle.Parent = this;
|
splitHandle.Parent = this;
|
||||||
|
|
||||||
splitHandle.Reprotect(handle.Dirty);
|
splitHandle.Reprotect(handle.Dirty);
|
||||||
|
@ -106,7 +107,7 @@ namespace Ryujinx.Memory.Tracking
|
||||||
// Fill any remaining space with new handles.
|
// Fill any remaining space with new handles.
|
||||||
while (i < _handles.Length)
|
while (i < _handles.Length)
|
||||||
{
|
{
|
||||||
RegionHandle handle = tracking.BeginTrackingBitmap(address + (ulong)i * granularity, granularity, _dirtyBitmap, i, id);
|
RegionHandle handle = tracking.BeginTrackingBitmap(address + (ulong)i * granularity, granularity, _dirtyBitmap, i, id, flags);
|
||||||
handle.Parent = this;
|
handle.Parent = this;
|
||||||
_handles[i++] = handle;
|
_handles[i++] = handle;
|
||||||
}
|
}
|
||||||
|
|
21
src/Ryujinx.Memory/Tracking/RegionFlags.cs
Normal file
21
src/Ryujinx.Memory/Tracking/RegionFlags.cs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Ryujinx.Memory.Tracking
|
||||||
|
{
|
||||||
|
[Flags]
|
||||||
|
public enum RegionFlags
|
||||||
|
{
|
||||||
|
None = 0,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Access to the resource is expected to occasionally be unaligned.
|
||||||
|
/// With some memory managers, guest protection must extend into the previous page to cover unaligned access.
|
||||||
|
/// If this is not expected, protection is not altered, which can avoid unintended resource dirty/flush.
|
||||||
|
/// </summary>
|
||||||
|
UnalignedAccess = 1,
|
||||||
|
}
|
||||||
|
}
|
|
@ -55,6 +55,8 @@ namespace Ryujinx.Memory.Tracking
|
||||||
private RegionSignal _preAction; // Action to perform before a read or write. This will block the memory access.
|
private RegionSignal _preAction; // Action to perform before a read or write. This will block the memory access.
|
||||||
private PreciseRegionSignal _preciseAction; // Action to perform on a precise read or write.
|
private PreciseRegionSignal _preciseAction; // Action to perform on a precise read or write.
|
||||||
private readonly List<VirtualRegion> _regions;
|
private readonly List<VirtualRegion> _regions;
|
||||||
|
private readonly List<VirtualRegion> _guestRegions;
|
||||||
|
private readonly List<VirtualRegion> _allRegions;
|
||||||
private readonly MemoryTracking _tracking;
|
private readonly MemoryTracking _tracking;
|
||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
|
|
||||||
|
@ -99,6 +101,7 @@ namespace Ryujinx.Memory.Tracking
|
||||||
/// <param name="bitmap">The bitmap the dirty flag for this handle is stored in</param>
|
/// <param name="bitmap">The bitmap the dirty flag for this handle is stored in</param>
|
||||||
/// <param name="bit">The bit index representing the dirty flag for this handle</param>
|
/// <param name="bit">The bit index representing the dirty flag for this handle</param>
|
||||||
/// <param name="id">Handle ID</param>
|
/// <param name="id">Handle ID</param>
|
||||||
|
/// <param name="flags">Region flags</param>
|
||||||
/// <param name="mapped">True if the region handle starts mapped</param>
|
/// <param name="mapped">True if the region handle starts mapped</param>
|
||||||
internal RegionHandle(
|
internal RegionHandle(
|
||||||
MemoryTracking tracking,
|
MemoryTracking tracking,
|
||||||
|
@ -109,6 +112,7 @@ namespace Ryujinx.Memory.Tracking
|
||||||
ConcurrentBitmap bitmap,
|
ConcurrentBitmap bitmap,
|
||||||
int bit,
|
int bit,
|
||||||
int id,
|
int id,
|
||||||
|
RegionFlags flags,
|
||||||
bool mapped = true)
|
bool mapped = true)
|
||||||
{
|
{
|
||||||
Bitmap = bitmap;
|
Bitmap = bitmap;
|
||||||
|
@ -128,11 +132,12 @@ namespace Ryujinx.Memory.Tracking
|
||||||
RealEndAddress = realAddress + realSize;
|
RealEndAddress = realAddress + realSize;
|
||||||
|
|
||||||
_tracking = tracking;
|
_tracking = tracking;
|
||||||
_regions = tracking.GetVirtualRegionsForHandle(address, size);
|
|
||||||
foreach (var region in _regions)
|
_regions = tracking.GetVirtualRegionsForHandle(address, size, false);
|
||||||
{
|
_guestRegions = GetGuestRegions(tracking, address, size, flags);
|
||||||
region.Handles.Add(this);
|
_allRegions = new List<VirtualRegion>(_regions.Count + _guestRegions.Count);
|
||||||
}
|
|
||||||
|
InitializeRegions();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -145,8 +150,9 @@ namespace Ryujinx.Memory.Tracking
|
||||||
/// <param name="realAddress">The real, unaligned address of the handle</param>
|
/// <param name="realAddress">The real, unaligned address of the handle</param>
|
||||||
/// <param name="realSize">The real, unaligned size of the handle</param>
|
/// <param name="realSize">The real, unaligned size of the handle</param>
|
||||||
/// <param name="id">Handle ID</param>
|
/// <param name="id">Handle ID</param>
|
||||||
|
/// <param name="flags">Region flags</param>
|
||||||
/// <param name="mapped">True if the region handle starts mapped</param>
|
/// <param name="mapped">True if the region handle starts mapped</param>
|
||||||
internal RegionHandle(MemoryTracking tracking, ulong address, ulong size, ulong realAddress, ulong realSize, int id, bool mapped = true)
|
internal RegionHandle(MemoryTracking tracking, ulong address, ulong size, ulong realAddress, ulong realSize, int id, RegionFlags flags, bool mapped = true)
|
||||||
{
|
{
|
||||||
Bitmap = new ConcurrentBitmap(1, mapped);
|
Bitmap = new ConcurrentBitmap(1, mapped);
|
||||||
|
|
||||||
|
@ -163,8 +169,37 @@ namespace Ryujinx.Memory.Tracking
|
||||||
RealEndAddress = realAddress + realSize;
|
RealEndAddress = realAddress + realSize;
|
||||||
|
|
||||||
_tracking = tracking;
|
_tracking = tracking;
|
||||||
_regions = tracking.GetVirtualRegionsForHandle(address, size);
|
|
||||||
foreach (var region in _regions)
|
_regions = tracking.GetVirtualRegionsForHandle(address, size, false);
|
||||||
|
_guestRegions = GetGuestRegions(tracking, address, size, flags);
|
||||||
|
_allRegions = new List<VirtualRegion>(_regions.Count + _guestRegions.Count);
|
||||||
|
|
||||||
|
InitializeRegions();
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<VirtualRegion> GetGuestRegions(MemoryTracking tracking, ulong address, ulong size, RegionFlags flags)
|
||||||
|
{
|
||||||
|
ulong guestAddress;
|
||||||
|
ulong guestSize;
|
||||||
|
|
||||||
|
if (flags.HasFlag(RegionFlags.UnalignedAccess))
|
||||||
|
{
|
||||||
|
(guestAddress, guestSize) = tracking.GetUnalignedSafeRegion(address, size);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
(guestAddress, guestSize) = (address, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tracking.GetVirtualRegionsForHandle(guestAddress, guestSize, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InitializeRegions()
|
||||||
|
{
|
||||||
|
_allRegions.AddRange(_regions);
|
||||||
|
_allRegions.AddRange(_guestRegions);
|
||||||
|
|
||||||
|
foreach (var region in _allRegions)
|
||||||
{
|
{
|
||||||
region.Handles.Add(this);
|
region.Handles.Add(this);
|
||||||
}
|
}
|
||||||
|
@ -321,7 +356,7 @@ namespace Ryujinx.Memory.Tracking
|
||||||
|
|
||||||
lock (_tracking.TrackingLock)
|
lock (_tracking.TrackingLock)
|
||||||
{
|
{
|
||||||
foreach (VirtualRegion region in _regions)
|
foreach (VirtualRegion region in _allRegions)
|
||||||
{
|
{
|
||||||
protectionChanged |= region.UpdateProtection();
|
protectionChanged |= region.UpdateProtection();
|
||||||
}
|
}
|
||||||
|
@ -379,7 +414,7 @@ namespace Ryujinx.Memory.Tracking
|
||||||
{
|
{
|
||||||
lock (_tracking.TrackingLock)
|
lock (_tracking.TrackingLock)
|
||||||
{
|
{
|
||||||
foreach (VirtualRegion region in _regions)
|
foreach (VirtualRegion region in _allRegions)
|
||||||
{
|
{
|
||||||
region.UpdateProtection();
|
region.UpdateProtection();
|
||||||
}
|
}
|
||||||
|
@ -413,10 +448,19 @@ namespace Ryujinx.Memory.Tracking
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="region">Virtual region to add as a child</param>
|
/// <param name="region">Virtual region to add as a child</param>
|
||||||
internal void AddChild(VirtualRegion region)
|
internal void AddChild(VirtualRegion region)
|
||||||
|
{
|
||||||
|
if (region.Guest)
|
||||||
|
{
|
||||||
|
_guestRegions.Add(region);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
_regions.Add(region);
|
_regions.Add(region);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_allRegions.Add(region);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Signal that this handle has been mapped or unmapped.
|
/// Signal that this handle has been mapped or unmapped.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -469,7 +513,7 @@ namespace Ryujinx.Memory.Tracking
|
||||||
|
|
||||||
lock (_tracking.TrackingLock)
|
lock (_tracking.TrackingLock)
|
||||||
{
|
{
|
||||||
foreach (VirtualRegion region in _regions)
|
foreach (VirtualRegion region in _allRegions)
|
||||||
{
|
{
|
||||||
region.RemoveHandle(this);
|
region.RemoveHandle(this);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,10 +13,14 @@ namespace Ryujinx.Memory.Tracking
|
||||||
private readonly MemoryTracking _tracking;
|
private readonly MemoryTracking _tracking;
|
||||||
private MemoryPermission _lastPermission;
|
private MemoryPermission _lastPermission;
|
||||||
|
|
||||||
public VirtualRegion(MemoryTracking tracking, ulong address, ulong size, MemoryPermission lastPermission = MemoryPermission.Invalid) : base(address, size)
|
public bool Guest { get; }
|
||||||
|
|
||||||
|
public VirtualRegion(MemoryTracking tracking, ulong address, ulong size, bool guest, MemoryPermission lastPermission = MemoryPermission.Invalid) : base(address, size)
|
||||||
{
|
{
|
||||||
_lastPermission = lastPermission;
|
_lastPermission = lastPermission;
|
||||||
_tracking = tracking;
|
_tracking = tracking;
|
||||||
|
|
||||||
|
Guest = guest;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
@ -103,7 +107,7 @@ namespace Ryujinx.Memory.Tracking
|
||||||
|
|
||||||
if (_lastPermission != permission)
|
if (_lastPermission != permission)
|
||||||
{
|
{
|
||||||
_tracking.ProtectVirtualRegion(this, permission);
|
_tracking.ProtectVirtualRegion(this, permission, Guest);
|
||||||
_lastPermission = permission;
|
_lastPermission = permission;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -131,7 +135,7 @@ namespace Ryujinx.Memory.Tracking
|
||||||
|
|
||||||
public override INonOverlappingRange Split(ulong splitAddress)
|
public override INonOverlappingRange Split(ulong splitAddress)
|
||||||
{
|
{
|
||||||
VirtualRegion newRegion = new(_tracking, splitAddress, EndAddress - splitAddress, _lastPermission);
|
VirtualRegion newRegion = new(_tracking, splitAddress, EndAddress - splitAddress, Guest, _lastPermission);
|
||||||
Size = splitAddress - Address;
|
Size = splitAddress - Address;
|
||||||
|
|
||||||
// The new region inherits all of our parents.
|
// The new region inherits all of our parents.
|
||||||
|
|
91
src/Ryujinx.Memory/VirtualMemoryManagerBase.cs
Normal file
91
src/Ryujinx.Memory/VirtualMemoryManagerBase.cs
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
using System;
|
||||||
|
using System.Numerics;
|
||||||
|
|
||||||
|
namespace Ryujinx.Memory
|
||||||
|
{
|
||||||
|
public abstract class VirtualMemoryManagerBase<TVirtual, TPhysical>
|
||||||
|
where TVirtual : IBinaryInteger<TVirtual>
|
||||||
|
where TPhysical : IBinaryInteger<TPhysical>
|
||||||
|
{
|
||||||
|
public const int PageBits = 12;
|
||||||
|
public const int PageSize = 1 << PageBits;
|
||||||
|
public const int PageMask = PageSize - 1;
|
||||||
|
|
||||||
|
protected abstract TVirtual AddressSpaceSize { get; }
|
||||||
|
|
||||||
|
public virtual void Read(TVirtual va, Span<byte> data)
|
||||||
|
{
|
||||||
|
if (data.Length == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
AssertValidAddressAndSize(va, TVirtual.CreateChecked(data.Length));
|
||||||
|
|
||||||
|
int offset = 0, size;
|
||||||
|
|
||||||
|
if ((int.CreateTruncating(va) & PageMask) != 0)
|
||||||
|
{
|
||||||
|
TPhysical pa = TranslateVirtualAddressForRead(va);
|
||||||
|
|
||||||
|
size = Math.Min(data.Length, PageSize - ((int.CreateTruncating(va) & PageMask)));
|
||||||
|
|
||||||
|
GetPhysicalAddressSpan(pa, size).CopyTo(data[..size]);
|
||||||
|
|
||||||
|
offset += size;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (; offset < data.Length; offset += size)
|
||||||
|
{
|
||||||
|
TPhysical pa = TranslateVirtualAddressForRead(va + TVirtual.CreateChecked(offset));
|
||||||
|
|
||||||
|
size = Math.Min(data.Length - offset, PageSize);
|
||||||
|
|
||||||
|
GetPhysicalAddressSpan(pa, size).CopyTo(data.Slice(offset, size));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ensures the combination of virtual address and size is part of the addressable space.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="va">Virtual address of the range</param>
|
||||||
|
/// <param name="size">Size of the range in bytes</param>
|
||||||
|
/// <exception cref="InvalidMemoryRegionException">Throw when the memory region specified outside the addressable space</exception>
|
||||||
|
protected void AssertValidAddressAndSize(TVirtual va, TVirtual size)
|
||||||
|
{
|
||||||
|
if (!ValidateAddressAndSize(va, size))
|
||||||
|
{
|
||||||
|
throw new InvalidMemoryRegionException($"va=0x{va:X16}, size=0x{size:X16}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract Span<byte> GetPhysicalAddressSpan(TPhysical pa, int size);
|
||||||
|
|
||||||
|
protected abstract TPhysical TranslateVirtualAddressForRead(TVirtual va);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if the virtual address is part of the addressable space.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="va">Virtual address</param>
|
||||||
|
/// <returns>True if the virtual address is part of the addressable space</returns>
|
||||||
|
protected bool ValidateAddress(TVirtual va)
|
||||||
|
{
|
||||||
|
return va < AddressSpaceSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if the combination of virtual address and size is part of the addressable space.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="va">Virtual address of the range</param>
|
||||||
|
/// <param name="size">Size of the range in bytes</param>
|
||||||
|
/// <returns>True if the combination of virtual address and size is part of the addressable space</returns>
|
||||||
|
protected bool ValidateAddressAndSize(TVirtual va, TVirtual size)
|
||||||
|
{
|
||||||
|
TVirtual endVa = va + size;
|
||||||
|
return endVa >= va && endVa >= size && endVa <= AddressSpaceSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void ThrowInvalidMemoryRegionException(string message)
|
||||||
|
=> throw new InvalidMemoryRegionException(message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -107,7 +107,7 @@ namespace Ryujinx.Tests.Memory
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void TrackingReprotect(ulong va, ulong size, MemoryPermission protection)
|
public void TrackingReprotect(ulong va, ulong size, MemoryPermission protection, bool guest)
|
||||||
{
|
{
|
||||||
OnProtect?.Invoke(va, size, protection);
|
OnProtect?.Invoke(va, size, protection);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ namespace Ryujinx.UI.Common
|
||||||
public static class DiscordIntegrationModule
|
public static class DiscordIntegrationModule
|
||||||
{
|
{
|
||||||
private const string Description = "A simple, experimental Nintendo Switch emulator.";
|
private const string Description = "A simple, experimental Nintendo Switch emulator.";
|
||||||
private const string CliendId = "568815339807309834";
|
private const string ApplicationId = "1216775165866807456";
|
||||||
|
|
||||||
private static DiscordRpcClient _discordClient;
|
private static DiscordRpcClient _discordClient;
|
||||||
private static RichPresence _discordPresenceMain;
|
private static RichPresence _discordPresenceMain;
|
||||||
|
@ -24,14 +24,14 @@ namespace Ryujinx.UI.Common
|
||||||
Details = "Main Menu",
|
Details = "Main Menu",
|
||||||
State = "Idling",
|
State = "Idling",
|
||||||
Timestamps = Timestamps.Now,
|
Timestamps = Timestamps.Now,
|
||||||
Buttons = new[]
|
Buttons =
|
||||||
{
|
[
|
||||||
new Button
|
new Button
|
||||||
{
|
{
|
||||||
Label = "Website",
|
Label = "Website",
|
||||||
Url = "https://ryujinx.org/",
|
Url = "https://ryujinx.org/",
|
||||||
},
|
},
|
||||||
},
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
ConfigurationState.Instance.EnableDiscordIntegration.Event += Update;
|
ConfigurationState.Instance.EnableDiscordIntegration.Event += Update;
|
||||||
|
@ -52,7 +52,7 @@ namespace Ryujinx.UI.Common
|
||||||
// If we need to activate it and the client isn't active, initialize it
|
// If we need to activate it and the client isn't active, initialize it
|
||||||
if (evnt.NewValue && _discordClient == null)
|
if (evnt.NewValue && _discordClient == null)
|
||||||
{
|
{
|
||||||
_discordClient = new DiscordRpcClient(CliendId);
|
_discordClient = new DiscordRpcClient(ApplicationId);
|
||||||
|
|
||||||
_discordClient.Initialize();
|
_discordClient.Initialize();
|
||||||
_discordClient.SetPresence(_discordPresenceMain);
|
_discordClient.SetPresence(_discordPresenceMain);
|
||||||
|
@ -74,14 +74,14 @@ namespace Ryujinx.UI.Common
|
||||||
Details = $"Playing {titleName}",
|
Details = $"Playing {titleName}",
|
||||||
State = (titleId == "0000000000000000") ? "Homebrew" : titleId.ToUpper(),
|
State = (titleId == "0000000000000000") ? "Homebrew" : titleId.ToUpper(),
|
||||||
Timestamps = Timestamps.Now,
|
Timestamps = Timestamps.Now,
|
||||||
Buttons = new[]
|
Buttons =
|
||||||
{
|
[
|
||||||
new Button
|
new Button
|
||||||
{
|
{
|
||||||
Label = "Website",
|
Label = "Website",
|
||||||
Url = "https://ryujinx.org/",
|
Url = "https://ryujinx.org/",
|
||||||
},
|
},
|
||||||
},
|
],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ using Ryujinx.Common.Configuration;
|
||||||
using Ryujinx.Common.Configuration.Multiplayer;
|
using Ryujinx.Common.Configuration.Multiplayer;
|
||||||
using Ryujinx.Common.Logging;
|
using Ryujinx.Common.Logging;
|
||||||
using Ryujinx.Common.SystemInterop;
|
using Ryujinx.Common.SystemInterop;
|
||||||
|
using Ryujinx.Common.Utilities;
|
||||||
using Ryujinx.Graphics.GAL;
|
using Ryujinx.Graphics.GAL;
|
||||||
using Ryujinx.Graphics.GAL.Multithreading;
|
using Ryujinx.Graphics.GAL.Multithreading;
|
||||||
using Ryujinx.Graphics.Gpu;
|
using Ryujinx.Graphics.Gpu;
|
||||||
|
@ -279,8 +280,11 @@ namespace Ryujinx.Ava
|
||||||
{
|
{
|
||||||
lock (_lockObject)
|
lock (_lockObject)
|
||||||
{
|
{
|
||||||
|
string applicationName = Device.Processes.ActiveApplication.Name;
|
||||||
|
string sanitizedApplicationName = FileSystemUtils.SanitizeFileName(applicationName);
|
||||||
DateTime currentTime = DateTime.Now;
|
DateTime currentTime = DateTime.Now;
|
||||||
string filename = $"ryujinx_capture_{currentTime.Year}-{currentTime.Month:D2}-{currentTime.Day:D2}_{currentTime.Hour:D2}-{currentTime.Minute:D2}-{currentTime.Second:D2}.png";
|
|
||||||
|
string filename = $"{sanitizedApplicationName}_{currentTime.Year}-{currentTime.Month:D2}-{currentTime.Day:D2}_{currentTime.Hour:D2}-{currentTime.Minute:D2}-{currentTime.Second:D2}.png";
|
||||||
|
|
||||||
string directory = AppDataManager.Mode switch
|
string directory = AppDataManager.Mode switch
|
||||||
{
|
{
|
||||||
|
|
|
@ -30,28 +30,22 @@ namespace Ryujinx.Ava.Common.Locale
|
||||||
Load();
|
Load();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Load()
|
private void Load()
|
||||||
{
|
{
|
||||||
// Load the system Language Code.
|
var localeLanguageCode = !string.IsNullOrEmpty(ConfigurationState.Instance.UI.LanguageCode.Value) ?
|
||||||
string localeLanguageCode = CultureInfo.CurrentCulture.Name.Replace('-', '_');
|
ConfigurationState.Instance.UI.LanguageCode.Value : CultureInfo.CurrentCulture.Name.Replace('-', '_');
|
||||||
|
|
||||||
// If the view is loaded with the UI Previewer detached, then override it with the saved one or default.
|
// Load en_US as default, if the target language translation is missing or incomplete.
|
||||||
|
LoadDefaultLanguage();
|
||||||
|
LoadLanguage(localeLanguageCode);
|
||||||
|
|
||||||
|
// Save whatever we ended up with.
|
||||||
if (Program.PreviewerDetached)
|
if (Program.PreviewerDetached)
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(ConfigurationState.Instance.UI.LanguageCode.Value))
|
ConfigurationState.Instance.UI.LanguageCode.Value = _localeLanguageCode;
|
||||||
{
|
|
||||||
localeLanguageCode = ConfigurationState.Instance.UI.LanguageCode.Value;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
localeLanguageCode = DefaultLanguageCode;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load en_US as default, if the target language translation is incomplete.
|
ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
|
||||||
LoadDefaultLanguage();
|
}
|
||||||
|
|
||||||
LoadLanguage(localeLanguageCode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public string this[LocaleKeys key]
|
public string this[LocaleKeys key]
|
||||||
|
@ -126,24 +120,42 @@ namespace Ryujinx.Ava.Common.Locale
|
||||||
|
|
||||||
private void LoadDefaultLanguage()
|
private void LoadDefaultLanguage()
|
||||||
{
|
{
|
||||||
_localeDefaultStrings = LoadJsonLanguage();
|
_localeDefaultStrings = LoadJsonLanguage(DefaultLanguageCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void LoadLanguage(string languageCode)
|
public void LoadLanguage(string languageCode)
|
||||||
{
|
{
|
||||||
foreach (var item in LoadJsonLanguage(languageCode))
|
var locale = LoadJsonLanguage(languageCode);
|
||||||
|
|
||||||
|
if (locale == null)
|
||||||
|
{
|
||||||
|
_localeLanguageCode = DefaultLanguageCode;
|
||||||
|
locale = _localeDefaultStrings;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_localeLanguageCode = languageCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var item in locale)
|
||||||
{
|
{
|
||||||
this[item.Key] = item.Value;
|
this[item.Key] = item.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
_localeLanguageCode = languageCode;
|
|
||||||
LocaleChanged?.Invoke();
|
LocaleChanged?.Invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Dictionary<LocaleKeys, string> LoadJsonLanguage(string languageCode = DefaultLanguageCode)
|
private static Dictionary<LocaleKeys, string> LoadJsonLanguage(string languageCode)
|
||||||
{
|
{
|
||||||
var localeStrings = new Dictionary<LocaleKeys, string>();
|
var localeStrings = new Dictionary<LocaleKeys, string>();
|
||||||
string languageJson = EmbeddedResources.ReadAllText($"Ryujinx/Assets/Locales/{languageCode}.json");
|
string languageJson = EmbeddedResources.ReadAllText($"Ryujinx/Assets/Locales/{languageCode}.json");
|
||||||
|
|
||||||
|
if (languageJson == null)
|
||||||
|
{
|
||||||
|
// We were unable to find file for that language code.
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
var strings = JsonHelper.Deserialize(languageJson, CommonJsonContext.Default.StringDictionary);
|
var strings = JsonHelper.Deserialize(languageJson, CommonJsonContext.Default.StringDictionary);
|
||||||
|
|
||||||
foreach (var item in strings)
|
foreach (var item in strings)
|
||||||
|
|
|
@ -298,7 +298,14 @@ namespace Ryujinx.Modules
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Find the process name.
|
// Find the process name.
|
||||||
string ryuName = Path.GetFileName(Environment.ProcessPath);
|
string ryuName = Path.GetFileName(Environment.ProcessPath) ?? string.Empty;
|
||||||
|
|
||||||
|
// Migration: Start the updated binary.
|
||||||
|
// TODO: Remove this in a future update.
|
||||||
|
if (ryuName.StartsWith("Ryujinx.Ava"))
|
||||||
|
{
|
||||||
|
ryuName = ryuName.Replace(".Ava", "");
|
||||||
|
}
|
||||||
|
|
||||||
// Some operating systems can see the renamed executable, so strip off the .ryuold if found.
|
// Some operating systems can see the renamed executable, so strip off the .ryuold if found.
|
||||||
if (ryuName.EndsWith(".ryuold"))
|
if (ryuName.EndsWith(".ryuold"))
|
||||||
|
@ -307,7 +314,7 @@ namespace Ryujinx.Modules
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback if the executable could not be found.
|
// Fallback if the executable could not be found.
|
||||||
if (!Path.Exists(Path.Combine(executableDirectory, ryuName)))
|
if (ryuName.Length == 0 || !Path.Exists(Path.Combine(executableDirectory, ryuName)))
|
||||||
{
|
{
|
||||||
ryuName = OperatingSystem.IsWindows() ? "Ryujinx.exe" : "Ryujinx";
|
ryuName = OperatingSystem.IsWindows() ? "Ryujinx.exe" : "Ryujinx";
|
||||||
}
|
}
|
||||||
|
@ -759,6 +766,43 @@ namespace Ryujinx.Modules
|
||||||
{
|
{
|
||||||
File.Delete(file);
|
File.Delete(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Migration: Delete old Ryujinx binary.
|
||||||
|
// TODO: Remove this in a future update.
|
||||||
|
if (!OperatingSystem.IsMacOS())
|
||||||
|
{
|
||||||
|
string[] oldRyuFiles = Directory.GetFiles(_homeDir, "Ryujinx.Ava*", SearchOption.TopDirectoryOnly);
|
||||||
|
// Assume we are running the new one if the process path is not available.
|
||||||
|
// This helps to prevent an infinite loop of restarts.
|
||||||
|
string currentRyuName = Path.GetFileName(Environment.ProcessPath) ?? (OperatingSystem.IsWindows() ? "Ryujinx.exe" : "Ryujinx");
|
||||||
|
|
||||||
|
string newRyuName = Path.Combine(_homeDir, currentRyuName.Replace(".Ava", ""));
|
||||||
|
if (!currentRyuName.Contains("Ryujinx.Ava"))
|
||||||
|
{
|
||||||
|
foreach (string oldRyuFile in oldRyuFiles)
|
||||||
|
{
|
||||||
|
File.Delete(oldRyuFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Should we be running the old binary, start the new one if possible.
|
||||||
|
else if (File.Exists(newRyuName))
|
||||||
|
{
|
||||||
|
ProcessStartInfo processStart = new(newRyuName)
|
||||||
|
{
|
||||||
|
UseShellExecute = true,
|
||||||
|
WorkingDirectory = _homeDir,
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (string argument in CommandLineState.Arguments)
|
||||||
|
{
|
||||||
|
processStart.ArgumentList.Add(argument);
|
||||||
|
}
|
||||||
|
|
||||||
|
Process.Start(processStart);
|
||||||
|
|
||||||
|
Environment.Exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,9 +56,6 @@
|
||||||
<PackageReference Include="SPB" />
|
<PackageReference Include="SPB" />
|
||||||
<PackageReference Include="SharpZipLib" />
|
<PackageReference Include="SharpZipLib" />
|
||||||
<PackageReference Include="SixLabors.ImageSharp" />
|
<PackageReference Include="SixLabors.ImageSharp" />
|
||||||
|
|
||||||
<!--NOTE: DO NOT REMOVE, THIS IS REQUIRED AS A RESULT OF A TRIMMING ISSUE IN AVALONIA -->
|
|
||||||
<PackageReference Include="System.Drawing.Common" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -8,6 +8,8 @@ namespace Ryujinx.Ava.UI.Helpers
|
||||||
[SupportedOSPlatform("windows")]
|
[SupportedOSPlatform("windows")]
|
||||||
internal partial class Win32NativeInterop
|
internal partial class Win32NativeInterop
|
||||||
{
|
{
|
||||||
|
internal const int GWLP_WNDPROC = -4;
|
||||||
|
|
||||||
[Flags]
|
[Flags]
|
||||||
public enum ClassStyles : uint
|
public enum ClassStyles : uint
|
||||||
{
|
{
|
||||||
|
@ -29,22 +31,7 @@ namespace Ryujinx.Ava.UI.Helpers
|
||||||
[SuppressMessage("Design", "CA1069: Enums values should not be duplicated")]
|
[SuppressMessage("Design", "CA1069: Enums values should not be duplicated")]
|
||||||
public enum WindowsMessages : uint
|
public enum WindowsMessages : uint
|
||||||
{
|
{
|
||||||
Mousemove = 0x0200,
|
NcHitTest = 0x0084,
|
||||||
Lbuttondown = 0x0201,
|
|
||||||
Lbuttonup = 0x0202,
|
|
||||||
Lbuttondblclk = 0x0203,
|
|
||||||
Rbuttondown = 0x0204,
|
|
||||||
Rbuttonup = 0x0205,
|
|
||||||
Rbuttondblclk = 0x0206,
|
|
||||||
Mbuttondown = 0x0207,
|
|
||||||
Mbuttonup = 0x0208,
|
|
||||||
Mbuttondblclk = 0x0209,
|
|
||||||
Mousewheel = 0x020A,
|
|
||||||
Xbuttondown = 0x020B,
|
|
||||||
Xbuttonup = 0x020C,
|
|
||||||
Xbuttondblclk = 0x020D,
|
|
||||||
Mousehwheel = 0x020E,
|
|
||||||
Mouselast = 0x020E,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
|
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
|
||||||
|
@ -121,5 +108,11 @@ namespace Ryujinx.Ava.UI.Helpers
|
||||||
IntPtr hMenu,
|
IntPtr hMenu,
|
||||||
IntPtr hInstance,
|
IntPtr hInstance,
|
||||||
IntPtr lpParam);
|
IntPtr lpParam);
|
||||||
|
|
||||||
|
[LibraryImport("user32.dll", SetLastError = true)]
|
||||||
|
public static partial IntPtr SetWindowLongPtrW(IntPtr hWnd, int nIndex, IntPtr value);
|
||||||
|
|
||||||
|
[LibraryImport("user32.dll", SetLastError = true)]
|
||||||
|
public static partial IntPtr SetWindowLongW(IntPtr hWnd, int nIndex, int value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -141,80 +141,10 @@ namespace Ryujinx.Ava.UI.Renderer
|
||||||
|
|
||||||
_wndProcDelegate = delegate (IntPtr hWnd, WindowsMessages msg, IntPtr wParam, IntPtr lParam)
|
_wndProcDelegate = delegate (IntPtr hWnd, WindowsMessages msg, IntPtr wParam, IntPtr lParam)
|
||||||
{
|
{
|
||||||
if (VisualRoot != null)
|
|
||||||
{
|
|
||||||
if (msg == WindowsMessages.Lbuttondown ||
|
|
||||||
msg == WindowsMessages.Rbuttondown ||
|
|
||||||
msg == WindowsMessages.Lbuttonup ||
|
|
||||||
msg == WindowsMessages.Rbuttonup ||
|
|
||||||
msg == WindowsMessages.Mousemove)
|
|
||||||
{
|
|
||||||
Point rootVisualPosition = this.TranslatePoint(new Point((long)lParam & 0xFFFF, (long)lParam >> 16 & 0xFFFF), this).Value;
|
|
||||||
Pointer pointer = new(0, PointerType.Mouse, true);
|
|
||||||
|
|
||||||
#pragma warning disable CS0618 // Type or member is obsolete (As of Avalonia 11, the constructors for PointerPressedEventArgs & PointerEventArgs are marked as obsolete)
|
|
||||||
switch (msg)
|
switch (msg)
|
||||||
{
|
{
|
||||||
case WindowsMessages.Lbuttondown:
|
case WindowsMessages.NcHitTest:
|
||||||
case WindowsMessages.Rbuttondown:
|
return -1;
|
||||||
{
|
|
||||||
bool isLeft = msg == WindowsMessages.Lbuttondown;
|
|
||||||
RawInputModifiers pointerPointModifier = isLeft ? RawInputModifiers.LeftMouseButton : RawInputModifiers.RightMouseButton;
|
|
||||||
PointerPointProperties properties = new(pointerPointModifier, isLeft ? PointerUpdateKind.LeftButtonPressed : PointerUpdateKind.RightButtonPressed);
|
|
||||||
|
|
||||||
var evnt = new PointerPressedEventArgs(
|
|
||||||
this,
|
|
||||||
pointer,
|
|
||||||
this,
|
|
||||||
rootVisualPosition,
|
|
||||||
(ulong)Environment.TickCount64,
|
|
||||||
properties,
|
|
||||||
KeyModifiers.None);
|
|
||||||
|
|
||||||
RaiseEvent(evnt);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case WindowsMessages.Lbuttonup:
|
|
||||||
case WindowsMessages.Rbuttonup:
|
|
||||||
{
|
|
||||||
bool isLeft = msg == WindowsMessages.Lbuttonup;
|
|
||||||
RawInputModifiers pointerPointModifier = isLeft ? RawInputModifiers.LeftMouseButton : RawInputModifiers.RightMouseButton;
|
|
||||||
PointerPointProperties properties = new(pointerPointModifier, isLeft ? PointerUpdateKind.LeftButtonReleased : PointerUpdateKind.RightButtonReleased);
|
|
||||||
|
|
||||||
var evnt = new PointerReleasedEventArgs(
|
|
||||||
this,
|
|
||||||
pointer,
|
|
||||||
this,
|
|
||||||
rootVisualPosition,
|
|
||||||
(ulong)Environment.TickCount64,
|
|
||||||
properties,
|
|
||||||
KeyModifiers.None,
|
|
||||||
isLeft ? MouseButton.Left : MouseButton.Right);
|
|
||||||
|
|
||||||
RaiseEvent(evnt);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case WindowsMessages.Mousemove:
|
|
||||||
{
|
|
||||||
var evnt = new PointerEventArgs(
|
|
||||||
PointerMovedEvent,
|
|
||||||
this,
|
|
||||||
pointer,
|
|
||||||
this,
|
|
||||||
rootVisualPosition,
|
|
||||||
(ulong)Environment.TickCount64,
|
|
||||||
new PointerPointProperties(RawInputModifiers.None, PointerUpdateKind.Other),
|
|
||||||
KeyModifiers.None);
|
|
||||||
|
|
||||||
RaiseEvent(evnt);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#pragma warning restore CS0618
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return DefWindowProc(hWnd, msg, wParam, lParam);
|
return DefWindowProc(hWnd, msg, wParam, lParam);
|
||||||
|
@ -234,6 +164,8 @@ namespace Ryujinx.Ava.UI.Renderer
|
||||||
|
|
||||||
WindowHandle = CreateWindowEx(0, _className, "NativeWindow", WindowStyles.WsChild, 0, 0, 640, 480, control.Handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
|
WindowHandle = CreateWindowEx(0, _className, "NativeWindow", WindowStyles.WsChild, 0, 0, 640, 480, control.Handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
|
||||||
|
|
||||||
|
SetWindowLongPtrW(control.Handle, GWLP_WNDPROC, wndClassEx.lpfnWndProc);
|
||||||
|
|
||||||
Marshal.FreeHGlobal(wndClassEx.lpszClassName);
|
Marshal.FreeHGlobal(wndClassEx.lpszClassName);
|
||||||
|
|
||||||
return new PlatformHandle(WindowHandle, "HWND");
|
return new PlatformHandle(WindowHandle, "HWND");
|
||||||
|
|
|
@ -9,7 +9,6 @@ namespace Ryujinx.Ava.UI.Windows
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
ExtendClientAreaToDecorationsHint = true;
|
|
||||||
TransparencyLevelHint = new[] { WindowTransparencyLevel.Transparent };
|
TransparencyLevelHint = new[] { WindowTransparencyLevel.Transparent };
|
||||||
WindowStartupLocation = WindowStartupLocation.Manual;
|
WindowStartupLocation = WindowStartupLocation.Manual;
|
||||||
SystemDecorations = SystemDecorations.None;
|
SystemDecorations = SystemDecorations.None;
|
||||||
|
|
|
@ -47,7 +47,7 @@ namespace Ryujinx.Ava.UI.Windows
|
||||||
|
|
||||||
contentDialog.Styles.Add(bottomBorder);
|
contentDialog.Styles.Add(bottomBorder);
|
||||||
|
|
||||||
await ContentDialogHelper.ShowAsync(contentDialog);
|
await contentDialog.ShowAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SaveAndClose(object sender, RoutedEventArgs routedEventArgs)
|
private void SaveAndClose(object sender, RoutedEventArgs routedEventArgs)
|
||||||
|
|
|
@ -127,7 +127,7 @@ namespace Ryujinx.Ava.UI.Windows
|
||||||
|
|
||||||
public static Bgra32[] GetBuffer(Image<Bgra32> image)
|
public static Bgra32[] GetBuffer(Image<Bgra32> image)
|
||||||
{
|
{
|
||||||
return image.TryGetSinglePixelSpan(out var data) ? data.ToArray() : Array.Empty<Bgra32>();
|
return image.DangerousTryGetSinglePixelMemory(out var data) ? data.ToArray() : Array.Empty<Bgra32>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int GetColorScore(Dictionary<int, int> dominantColorBin, int maxHitCount, PaletteColor color)
|
private static int GetColorScore(Dictionary<int, int> dominantColorBin, int maxHitCount, PaletteColor color)
|
||||||
|
|
|
@ -14,8 +14,8 @@
|
||||||
WindowState="{Binding WindowState}"
|
WindowState="{Binding WindowState}"
|
||||||
Width="{Binding WindowWidth}"
|
Width="{Binding WindowWidth}"
|
||||||
Height="{Binding WindowHeight}"
|
Height="{Binding WindowHeight}"
|
||||||
MinWidth="1092"
|
MinWidth="800"
|
||||||
MinHeight="672"
|
MinHeight="500"
|
||||||
d:DesignHeight="720"
|
d:DesignHeight="720"
|
||||||
d:DesignWidth="1280"
|
d:DesignWidth="1280"
|
||||||
x:DataType="viewModels:MainWindowViewModel"
|
x:DataType="viewModels:MainWindowViewModel"
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
|
using Avalonia;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Controls.ApplicationLifetimes;
|
||||||
using Avalonia.Interactivity;
|
using Avalonia.Interactivity;
|
||||||
using Avalonia.Styling;
|
using Avalonia.Styling;
|
||||||
using FluentAvalonia.UI.Controls;
|
using FluentAvalonia.UI.Controls;
|
||||||
|
@ -47,7 +49,7 @@ namespace Ryujinx.Ava.UI.Windows
|
||||||
|
|
||||||
contentDialog.Styles.Add(bottomBorder);
|
contentDialog.Styles.Add(bottomBorder);
|
||||||
|
|
||||||
await ContentDialogHelper.ShowAsync(contentDialog);
|
await contentDialog.ShowAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Close(object sender, RoutedEventArgs e)
|
private void Close(object sender, RoutedEventArgs e)
|
||||||
|
@ -59,9 +61,15 @@ namespace Ryujinx.Ava.UI.Windows
|
||||||
{
|
{
|
||||||
ViewModel.Save();
|
ViewModel.Save();
|
||||||
|
|
||||||
if (VisualRoot is MainWindow window)
|
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime al)
|
||||||
{
|
{
|
||||||
window.LoadApplications();
|
foreach (Window window in al.Windows)
|
||||||
|
{
|
||||||
|
if (window is MainWindow mainWindow)
|
||||||
|
{
|
||||||
|
mainWindow.LoadApplications();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
((ContentDialog)Parent).Hide();
|
((ContentDialog)Parent).Hide();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue