From f77694e4f774c9391aad5344e70a7c8721cfedc6 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Sun, 3 May 2020 19:54:50 -0300 Subject: [PATCH] Implement a new physical memory manager and replace DeviceMemory (#856) * Implement a new physical memory manager and replace DeviceMemory * Proper generic constraints * Fix debug build * Add memory tests * New CPU memory manager and general code cleanup * Remove host memory management from CPU project, use Ryujinx.Memory instead * Fix tests * Document exceptions on MemoryBlock * Fix leak on unix memory allocation * Proper disposal of some objects on tests * Fix JitCache not being set as initialized * GetRef without checks for 8-bits and 16-bits CAS * Add MemoryBlock destructor * Throw in separate method to improve codegen * Address PR feedback * QueryModified improvements * Fix memory write tracking not marking all pages as modified in some cases * Simplify MarkRegionAsModified * Remove XML doc for ghost param * Add back optimization to avoid useless buffer updates * Add Ryujinx.Cpu project, move MemoryManager there and remove MemoryBlockWrapper * Some nits * Do not perform address translation when size is 0 * Address PR feedback and format NativeInterface class * Remove ghost parameter description * Update Ryujinx.Cpu to .NET Core 3.1 * Address PR feedback * Fix build * Return a well defined value for GetPhysicalAddress with invalid VA, and do not return unmapped ranges as modified * Typo --- .../CodeGen/X86/HardwareCapabilities.cs | 53 +- ARMeilleure/Decoders/Decoder.cs | 18 +- .../Instructions/InstEmitMemoryHelper.cs | 32 +- ARMeilleure/Instructions/NativeInterface.cs | 143 ++-- ARMeilleure/Memory/IJitMemoryAllocator.cs | 8 + ARMeilleure/Memory/IJitMemoryBlock.cs | 14 + ARMeilleure/Memory/IMemoryManager.cs | 19 + ARMeilleure/Memory/MemoryManagementUnix.cs | 94 --- ARMeilleure/Memory/MemoryManagementWindows.cs | 142 ---- ARMeilleure/Memory/MemoryManager.cs | 738 ------------------ ARMeilleure/Memory/MemoryManagerPal.cs | 11 +- ARMeilleure/Memory/MemoryProtection.cs | 17 - .../Memory/MemoryProtectionException.cs | 9 - ARMeilleure/Memory/ReservedRegion.cs | 16 +- ARMeilleure/State/ExecutionContext.cs | 5 +- ARMeilleure/State/NativeContext.cs | 10 +- ARMeilleure/Translation/ArmEmitterContext.cs | 4 +- ARMeilleure/Translation/Compiler.cs | 4 +- ARMeilleure/Translation/DirectCallStubs.cs | 19 +- ARMeilleure/Translation/JitCache.cs | 51 +- ARMeilleure/Translation/JumpTable.cs | 22 +- ARMeilleure/Translation/Translator.cs | 28 +- Ryujinx.Cpu/CpuContext.cs | 19 + Ryujinx.Cpu/JitMemoryAllocator.cs | 11 + Ryujinx.Cpu/JitMemoryBlock.cs | 24 + .../Memory => Ryujinx.Cpu}/MemoryHelper.cs | 16 +- Ryujinx.Cpu/MemoryManager.cs | 466 +++++++++++ Ryujinx.Cpu/MemoryNotContiguousException.cs | 19 + Ryujinx.Cpu/Ryujinx.Cpu.csproj | 20 + Ryujinx.Graphics.Gpu/GpuContext.cs | 2 +- Ryujinx.Graphics.Gpu/Image/Pool.cs | 15 +- Ryujinx.Graphics.Gpu/Image/Texture.cs | 12 +- Ryujinx.Graphics.Gpu/Memory/Buffer.cs | 12 +- Ryujinx.Graphics.Gpu/Memory/PhysicalMemory.cs | 29 +- .../Ryujinx.Graphics.Gpu.csproj | 2 +- Ryujinx.HLE/DeviceMemory.cs | 200 ----- Ryujinx.HLE/HOS/Font/SharedFontManager.cs | 18 +- Ryujinx.HLE/HOS/Homebrew.cs | 77 -- Ryujinx.HLE/HOS/Horizon.cs | 8 +- Ryujinx.HLE/HOS/Ipc/IpcHandler.cs | 4 +- .../HOS/Kernel/Common/KernelTransfer.cs | 30 +- Ryujinx.HLE/HOS/Kernel/Ipc/KServerSession.cs | 88 +-- .../HOS/Kernel/Memory/KMemoryManager.cs | 18 +- .../HOS/Kernel/Process/HleProcessDebugger.cs | 132 ++-- Ryujinx.HLE/HOS/Kernel/Process/KProcess.cs | 13 +- .../HOS/Kernel/SupervisorCall/SvcIpc.cs | 4 +- .../HOS/Kernel/SupervisorCall/SvcMemory.cs | 16 +- .../HOS/Kernel/SupervisorCall/SvcSystem.cs | 2 +- .../HOS/Kernel/SupervisorCall/SvcThread.cs | 140 ++-- .../Kernel/SupervisorCall/SvcThreadSync.cs | 2 +- .../HOS/Kernel/Threading/KAddressArbiter.cs | 61 +- Ryujinx.HLE/HOS/Kernel/Threading/KThread.cs | 6 +- Ryujinx.HLE/HOS/ProgramLoader.cs | 9 +- Ryujinx.HLE/HOS/ServiceCtx.cs | 2 +- .../Acc/IAccountServiceForApplication.cs | 10 +- .../HOS/Services/Account/Acc/IProfile.cs | 10 +- .../Services/Am/AppletAE/IStorageAccessor.cs | 6 +- .../Audio/AudioOutManager/IAudioOut.cs | 10 +- .../AudioRendererManager/IAudioDevice.cs | 16 +- .../AudioRendererManager/IAudioRenderer.cs | 4 +- .../AudioRendererManager/VoiceContext.cs | 10 +- .../IHardwareOpusDecoder.cs | 32 +- .../HOS/Services/Audio/IAudioOutManager.cs | 6 +- .../IDeliveryCacheDirectoryService.cs | 2 +- .../IDeliveryCacheFileService.cs | 2 +- .../IDeliveryCacheProgressService.cs | 2 +- .../IDeliveryCacheStorageService.cs | 2 +- .../Friend/ServiceCreator/IFriendService.cs | 4 +- .../FileSystemProxy/FileSystemProxyHelper.cs | 4 +- .../Services/Fs/FileSystemProxy/IDirectory.cs | 2 +- .../HOS/Services/Fs/FileSystemProxy/IFile.cs | 6 +- .../Services/Fs/FileSystemProxy/IStorage.cs | 2 +- .../HOS/Services/Fs/IFileSystemProxy.cs | 2 +- .../HOS/Services/Fs/ISaveDataInfoReader.cs | 2 +- Ryujinx.HLE/HOS/Services/Hid/Hid.cs | 10 +- Ryujinx.HLE/HOS/Services/Hid/IHidServer.cs | 14 +- .../HOS/Services/Lm/LogService/ILogger.cs | 5 +- .../Mii/StaticService/IDatabaseService.cs | 8 +- .../ILocationResolver.cs | 2 +- .../HOS/Services/Nfc/Nfp/UserManager/IUser.cs | 6 +- .../Nifm/StaticService/IGeneralService.cs | 4 +- .../Ns/IApplicationManagerInterface.cs | 2 +- ...ReadOnlyApplicationControlDataInterface.cs | 2 +- Ryujinx.HLE/HOS/Services/Nv/INvDrvServices.cs | 32 +- .../NvHostChannel/NvHostChannelDeviceFile.cs | 4 +- .../HOS/Services/Prepo/IPrepoService.cs | 4 +- Ryujinx.HLE/HOS/Services/Ro/IRoInterface.cs | 26 +- .../QueryPlayStatisticsManager.cs | 6 +- .../HOS/Services/Sdb/Pl/ISharedFontManager.cs | 8 +- .../HOS/Services/Settings/ISettingsServer.cs | 2 +- .../Settings/ISystemSettingsServer.cs | 28 +- .../HOS/Services/Sockets/Bsd/IClient.cs | 68 +- .../HOS/Services/Sockets/Nsd/IManager.cs | 8 +- .../Sockets/Nsd/Manager/FqdnResolver.cs | 7 +- .../Services/Sockets/Sfdnsres/IResolver.cs | 29 +- .../HOS/Services/Spl/IRandomInterface.cs | 2 +- .../SurfaceFlinger/IHOSBinderDriver.cs | 8 +- .../HOS/Services/Time/IStaticServiceForPsc.cs | 8 +- .../HOS/Services/Time/ITimeServiceManager.cs | 6 +- .../StaticService/ITimeZoneServiceForGlue.cs | 5 +- .../StaticService/ITimeZoneServiceForPsc.cs | 20 +- Ryujinx.HLE/HOS/Services/Time/TimeManager.cs | 2 +- .../HOS/Services/Time/TimeSharedMemory.cs | 31 +- .../RootService/IApplicationDisplayService.cs | 16 +- Ryujinx.HLE/Loaders/Elf/ElfSymbol32.cs | 4 +- Ryujinx.HLE/Loaders/Elf/ElfSymbol64.cs | 4 +- Ryujinx.HLE/Ryujinx.HLE.csproj | 2 + Ryujinx.HLE/Switch.cs | 5 +- Ryujinx.HLE/Utilities/StringUtils.cs | 8 +- Ryujinx.HLE/Utilities/StructReader.cs | 2 +- Ryujinx.HLE/Utilities/StructWriter.cs | 2 +- .../Ryujinx.Memory.Tests.csproj | 19 + Ryujinx.Memory.Tests/Tests.cs | 42 + Ryujinx.Memory/MemoryAllocationFlags.cs | 28 + Ryujinx.Memory/MemoryBlock.cs | 276 +++++++ Ryujinx.Memory/MemoryConstants.cs | 9 + .../MemoryManagement.cs | 32 +- Ryujinx.Memory/MemoryManagementUnix.cs | 75 ++ Ryujinx.Memory/MemoryManagementWindows.cs | 106 +++ Ryujinx.Memory/MemoryPermission.cs | 46 ++ Ryujinx.Memory/MemoryProtectionException.cs | 23 + Ryujinx.Memory/Ryujinx.Memory.csproj | 23 + Ryujinx.Tests/Cpu/CpuTest.cs | 39 +- Ryujinx.Tests/Cpu/CpuTest32.cs | 50 +- Ryujinx.Tests/Ryujinx.Tests.csproj | 2 + Ryujinx.sln | 30 + 126 files changed, 2176 insertions(+), 2092 deletions(-) create mode 100644 ARMeilleure/Memory/IJitMemoryAllocator.cs create mode 100644 ARMeilleure/Memory/IJitMemoryBlock.cs create mode 100644 ARMeilleure/Memory/IMemoryManager.cs delete mode 100644 ARMeilleure/Memory/MemoryManagementUnix.cs delete mode 100644 ARMeilleure/Memory/MemoryManagementWindows.cs delete mode 100644 ARMeilleure/Memory/MemoryManager.cs delete mode 100644 ARMeilleure/Memory/MemoryProtection.cs delete mode 100644 ARMeilleure/Memory/MemoryProtectionException.cs create mode 100644 Ryujinx.Cpu/CpuContext.cs create mode 100644 Ryujinx.Cpu/JitMemoryAllocator.cs create mode 100644 Ryujinx.Cpu/JitMemoryBlock.cs rename {ARMeilleure/Memory => Ryujinx.Cpu}/MemoryHelper.cs (80%) create mode 100644 Ryujinx.Cpu/MemoryManager.cs create mode 100644 Ryujinx.Cpu/MemoryNotContiguousException.cs create mode 100644 Ryujinx.Cpu/Ryujinx.Cpu.csproj delete mode 100644 Ryujinx.HLE/DeviceMemory.cs delete mode 100644 Ryujinx.HLE/HOS/Homebrew.cs create mode 100644 Ryujinx.Memory.Tests/Ryujinx.Memory.Tests.csproj create mode 100644 Ryujinx.Memory.Tests/Tests.cs create mode 100644 Ryujinx.Memory/MemoryAllocationFlags.cs create mode 100644 Ryujinx.Memory/MemoryBlock.cs create mode 100644 Ryujinx.Memory/MemoryConstants.cs rename {ARMeilleure/Memory => Ryujinx.Memory}/MemoryManagement.cs (81%) create mode 100644 Ryujinx.Memory/MemoryManagementUnix.cs create mode 100644 Ryujinx.Memory/MemoryManagementWindows.cs create mode 100644 Ryujinx.Memory/MemoryPermission.cs create mode 100644 Ryujinx.Memory/MemoryProtectionException.cs create mode 100644 Ryujinx.Memory/Ryujinx.Memory.csproj diff --git a/ARMeilleure/CodeGen/X86/HardwareCapabilities.cs b/ARMeilleure/CodeGen/X86/HardwareCapabilities.cs index ed814829..b622c65c 100644 --- a/ARMeilleure/CodeGen/X86/HardwareCapabilities.cs +++ b/ARMeilleure/CodeGen/X86/HardwareCapabilities.cs @@ -1,52 +1,23 @@ -using ARMeilleure.IntermediateRepresentation; -using ARMeilleure.Translation; +using System.Runtime.Intrinsics.X86; namespace ARMeilleure.CodeGen.X86 { static class HardwareCapabilities { - private delegate ulong GetFeatureInfo(); - - private static ulong _featureInfo; - - public static bool SupportsSse3 => (_featureInfo & (1UL << 0)) != 0; - public static bool SupportsPclmulqdq => (_featureInfo & (1UL << 1)) != 0; - public static bool SupportsSsse3 => (_featureInfo & (1UL << 9)) != 0; - public static bool SupportsFma => (_featureInfo & (1UL << 12)) != 0; - public static bool SupportsCx16 => (_featureInfo & (1UL << 13)) != 0; - public static bool SupportsSse41 => (_featureInfo & (1UL << 19)) != 0; - public static bool SupportsSse42 => (_featureInfo & (1UL << 20)) != 0; - public static bool SupportsPopcnt => (_featureInfo & (1UL << 23)) != 0; - public static bool SupportsAesni => (_featureInfo & (1UL << 25)) != 0; - public static bool SupportsAvx => (_featureInfo & (1UL << 28)) != 0; - public static bool SupportsF16c => (_featureInfo & (1UL << 29)) != 0; - - public static bool SupportsSse => (_featureInfo & (1UL << 32 + 25)) != 0; - public static bool SupportsSse2 => (_featureInfo & (1UL << 32 + 26)) != 0; + public static bool SupportsSse => Sse.IsSupported; + public static bool SupportsSse2 => Sse2.IsSupported; + public static bool SupportsSse3 => Sse3.IsSupported; + public static bool SupportsSsse3 => Ssse3.IsSupported; + public static bool SupportsSse41 => Sse41.IsSupported; + public static bool SupportsSse42 => Sse42.IsSupported; + public static bool SupportsPclmulqdq => Pclmulqdq.IsSupported; + public static bool SupportsFma => Fma.IsSupported; + public static bool SupportsPopcnt => Popcnt.IsSupported; + public static bool SupportsAesni => Aes.IsSupported; + public static bool SupportsAvx => Avx.IsSupported; public static bool ForceLegacySse { get; set; } public static bool SupportsVexEncoding => SupportsAvx && !ForceLegacySse; - - static HardwareCapabilities() - { - EmitterContext context = new EmitterContext(); - - Operand featureInfo = context.CpuId(); - - context.Return(featureInfo); - - ControlFlowGraph cfg = context.GetControlFlowGraph(); - - OperandType[] argTypes = new OperandType[0]; - - GetFeatureInfo getFeatureInfo = Compiler.Compile( - cfg, - argTypes, - OperandType.I64, - CompilerOptions.HighCq); - - _featureInfo = getFeatureInfo(); - } } } \ No newline at end of file diff --git a/ARMeilleure/Decoders/Decoder.cs b/ARMeilleure/Decoders/Decoder.cs index dd99f78f..5ba12fb7 100644 --- a/ARMeilleure/Decoders/Decoder.cs +++ b/ARMeilleure/Decoders/Decoder.cs @@ -17,7 +17,7 @@ namespace ARMeilleure.Decoders // For lower code quality translation, we set a lower limit since we're blocking execution. private const int MaxInstsPerFunctionLowCq = 500; - public static Block[] DecodeBasicBlock(MemoryManager memory, ulong address, ExecutionMode mode) + public static Block[] DecodeBasicBlock(IMemoryManager memory, ulong address, ExecutionMode mode) { Block block = new Block(address); @@ -26,7 +26,7 @@ namespace ARMeilleure.Decoders return new Block[] { block }; } - public static Block[] DecodeFunction(MemoryManager memory, ulong address, ExecutionMode mode, bool highCq) + public static Block[] DecodeFunction(IMemoryManager memory, ulong address, ExecutionMode mode, bool highCq) { List blocks = new List(); @@ -42,7 +42,7 @@ namespace ARMeilleure.Decoders { if (!visited.TryGetValue(blkAddress, out Block block)) { - if (opsCount > instructionLimit || !memory.IsMapped((long)blkAddress)) + if (opsCount > instructionLimit || !memory.IsMapped(blkAddress)) { return null; } @@ -176,10 +176,10 @@ namespace ARMeilleure.Decoders } private static void FillBlock( - MemoryManager memory, - ExecutionMode mode, - Block block, - ulong limitAddress) + IMemoryManager memory, + ExecutionMode mode, + Block block, + ulong limitAddress) { ulong address = block.Address; @@ -302,9 +302,9 @@ namespace ARMeilleure.Decoders opCode.Instruction.Name == InstName.Und; } - public static OpCode DecodeOpCode(MemoryManager memory, ulong address, ExecutionMode mode) + public static OpCode DecodeOpCode(IMemoryManager memory, ulong address, ExecutionMode mode) { - int opCode = memory.ReadInt32((long)address); + int opCode = memory.Read(address); InstDescriptor inst; diff --git a/ARMeilleure/Instructions/InstEmitMemoryHelper.cs b/ARMeilleure/Instructions/InstEmitMemoryHelper.cs index e1dec331..b6a4d391 100644 --- a/ARMeilleure/Instructions/InstEmitMemoryHelper.cs +++ b/ARMeilleure/Instructions/InstEmitMemoryHelper.cs @@ -1,6 +1,5 @@ using ARMeilleure.Decoders; using ARMeilleure.IntermediateRepresentation; -using ARMeilleure.Memory; using ARMeilleure.Translation; using System; @@ -11,6 +10,9 @@ namespace ARMeilleure.Instructions { static class InstEmitMemoryHelper { + private const int PageBits = 12; + private const int PageMask = (1 << PageBits) - 1; + private enum Extension { Zx, @@ -318,28 +320,32 @@ namespace ARMeilleure.Instructions private static Operand EmitAddressCheck(ArmEmitterContext context, Operand address, int size) { - long addressCheckMask = ~(context.Memory.AddressSpaceSize - 1); + ulong addressCheckMask = ~((1UL << context.Memory.AddressSpaceBits) - 1); addressCheckMask |= (1u << size) - 1; - return context.BitwiseAnd(address, Const(address.Type, addressCheckMask)); + return context.BitwiseAnd(address, Const(address.Type, (long)addressCheckMask)); } - private static Operand EmitPtPointerLoad(ArmEmitterContext context, Operand address, Operand lblFallbackPath) + private static Operand EmitPtPointerLoad(ArmEmitterContext context, Operand address, Operand lblSlowPath) { - Operand pte = Const(context.Memory.PageTable.ToInt64()); + int ptLevelBits = context.Memory.AddressSpaceBits - 12; // 12 = Number of page bits. + int ptLevelSize = 1 << ptLevelBits; + int ptLevelMask = ptLevelSize - 1; - int bit = MemoryManager.PageBits; + Operand pte = Const(context.Memory.PageTablePointer.ToInt64()); + + int bit = PageBits; do { Operand addrPart = context.ShiftRightUI(address, Const(bit)); - bit += context.Memory.PtLevelBits; + bit += ptLevelBits; if (bit < context.Memory.AddressSpaceBits) { - addrPart = context.BitwiseAnd(addrPart, Const(addrPart.Type, context.Memory.PtLevelMask)); + addrPart = context.BitwiseAnd(addrPart, Const(addrPart.Type, ptLevelMask)); } Operand pteOffset = context.ShiftLeft(addrPart, Const(3)); @@ -355,20 +361,16 @@ namespace ARMeilleure.Instructions } while (bit < context.Memory.AddressSpaceBits); - Operand hasFlagSet = context.BitwiseAnd(pte, Const((long)MemoryManager.PteFlagsMask)); + context.BranchIfTrue(lblSlowPath, context.ICompareLess(pte, Const(0L))); - context.BranchIfTrue(lblFallbackPath, hasFlagSet); - - Operand pageOffset = context.BitwiseAnd(address, Const(address.Type, MemoryManager.PageMask)); + Operand pageOffset = context.BitwiseAnd(address, Const(address.Type, PageMask)); if (pageOffset.Type == OperandType.I32) { pageOffset = context.ZeroExtend32(OperandType.I64, pageOffset); } - Operand physAddr = context.Add(pte, pageOffset); - - return physAddr; + return context.Add(pte, pageOffset); } private static void EmitReadIntFallback(ArmEmitterContext context, Operand address, int rt, int size) diff --git a/ARMeilleure/Instructions/NativeInterface.cs b/ARMeilleure/Instructions/NativeInterface.cs index 610a1209..f42dc283 100644 --- a/ARMeilleure/Instructions/NativeInterface.cs +++ b/ARMeilleure/Instructions/NativeInterface.cs @@ -3,6 +3,7 @@ using ARMeilleure.State; using ARMeilleure.Translation; using System; using System.Runtime.InteropServices; +using System.Threading; namespace ARMeilleure.Instructions { @@ -12,18 +13,18 @@ namespace ARMeilleure.Instructions private class ThreadContext { - public ExecutionContext Context { get; } - public MemoryManager Memory { get; } - public Translator Translator { get; } + public State.ExecutionContext Context { get; } + public IMemoryManager Memory { get; } + public Translator Translator { get; } - public ulong ExclusiveAddress { get; set; } - public ulong ExclusiveValueLow { get; set; } + public ulong ExclusiveAddress { get; set; } + public ulong ExclusiveValueLow { get; set; } public ulong ExclusiveValueHigh { get; set; } - public ThreadContext(ExecutionContext context, MemoryManager memory, Translator translator) + public ThreadContext(State.ExecutionContext context, IMemoryManager memory, Translator translator) { - Context = context; - Memory = memory; + Context = context; + Memory = memory; Translator = translator; ExclusiveAddress = ulong.MaxValue; @@ -33,7 +34,7 @@ namespace ARMeilleure.Instructions [ThreadStatic] private static ThreadContext _context; - public static void RegisterThread(ExecutionContext context, MemoryManager memory, Translator translator) + public static void RegisterThread(State.ExecutionContext context, IMemoryManager memory, Translator translator) { _context = new ThreadContext(context, memory, translator); } @@ -70,7 +71,7 @@ namespace ARMeilleure.Instructions Statistics.ResumeTimer(); } -#region "System registers" + #region "System registers" public static ulong GetCtrEl0() { return (ulong)GetContext().CtrEl0; @@ -93,7 +94,8 @@ namespace ARMeilleure.Instructions public static uint GetFpscr() { - ExecutionContext context = GetContext(); + var context = GetContext(); + uint result = (uint)(context.Fpsr & FPSR.A32Mask) | (uint)(context.Fpcr & FPCR.A32Mask); result |= context.GetFPstateFlag(FPState.NFlag) ? (1u << 31) : 0; @@ -146,7 +148,7 @@ namespace ARMeilleure.Instructions public static void SetFpscr(uint value) { - ExecutionContext context = GetContext(); + var context = GetContext(); context.SetFPstateFlag(FPState.NFlag, (value & (1u << 31)) != 0); context.SetFPstateFlag(FPState.ZFlag, (value & (1u << 30)) != 0); @@ -171,37 +173,37 @@ namespace ARMeilleure.Instructions #region "Read" public static byte ReadByte(ulong address) { - return GetMemoryManager().ReadByte((long)address); + return GetMemoryManager().Read(address); } public static ushort ReadUInt16(ulong address) { - return GetMemoryManager().ReadUInt16((long)address); + return GetMemoryManager().Read(address); } public static uint ReadUInt32(ulong address) { - return GetMemoryManager().ReadUInt32((long)address); + return GetMemoryManager().Read(address); } public static ulong ReadUInt64(ulong address) { - return GetMemoryManager().ReadUInt64((long)address); + return GetMemoryManager().Read(address); } public static V128 ReadVector128(ulong address) { - return GetMemoryManager().ReadVector128((long)address); + return GetMemoryManager().Read(address); } -#endregion + #endregion -#region "Read exclusive" + #region "Read exclusive" public static byte ReadByteExclusive(ulong address) { - byte value = _context.Memory.ReadByte((long)address); + byte value = _context.Memory.Read(address); - _context.ExclusiveAddress = GetMaskedExclusiveAddress(address); - _context.ExclusiveValueLow = value; + _context.ExclusiveAddress = GetMaskedExclusiveAddress(address); + _context.ExclusiveValueLow = value; _context.ExclusiveValueHigh = 0; return value; @@ -209,10 +211,10 @@ namespace ARMeilleure.Instructions public static ushort ReadUInt16Exclusive(ulong address) { - ushort value = _context.Memory.ReadUInt16((long)address); + ushort value = _context.Memory.Read(address); - _context.ExclusiveAddress = GetMaskedExclusiveAddress(address); - _context.ExclusiveValueLow = value; + _context.ExclusiveAddress = GetMaskedExclusiveAddress(address); + _context.ExclusiveValueLow = value; _context.ExclusiveValueHigh = 0; return value; @@ -220,10 +222,10 @@ namespace ARMeilleure.Instructions public static uint ReadUInt32Exclusive(ulong address) { - uint value = _context.Memory.ReadUInt32((long)address); + uint value = _context.Memory.Read(address); - _context.ExclusiveAddress = GetMaskedExclusiveAddress(address); - _context.ExclusiveValueLow = value; + _context.ExclusiveAddress = GetMaskedExclusiveAddress(address); + _context.ExclusiveValueLow = value; _context.ExclusiveValueHigh = 0; return value; @@ -231,10 +233,10 @@ namespace ARMeilleure.Instructions public static ulong ReadUInt64Exclusive(ulong address) { - ulong value = _context.Memory.ReadUInt64((long)address); + ulong value = _context.Memory.Read(address); - _context.ExclusiveAddress = GetMaskedExclusiveAddress(address); - _context.ExclusiveValueLow = value; + _context.ExclusiveAddress = GetMaskedExclusiveAddress(address); + _context.ExclusiveValueLow = value; _context.ExclusiveValueHigh = 0; return value; @@ -242,54 +244,60 @@ namespace ARMeilleure.Instructions public static V128 ReadVector128Exclusive(ulong address) { - V128 value = _context.Memory.AtomicLoadInt128((long)address); + V128 value = MemoryManagerPal.AtomicLoad128(ref _context.Memory.GetRef(address)); - _context.ExclusiveAddress = GetMaskedExclusiveAddress(address); - _context.ExclusiveValueLow = value.Extract(0); + _context.ExclusiveAddress = GetMaskedExclusiveAddress(address); + _context.ExclusiveValueLow = value.Extract(0); _context.ExclusiveValueHigh = value.Extract(1); return value; } -#endregion + #endregion -#region "Write" + #region "Write" public static void WriteByte(ulong address, byte value) { - GetMemoryManager().WriteByte((long)address, value); + GetMemoryManager().Write(address, value); } public static void WriteUInt16(ulong address, ushort value) { - GetMemoryManager().WriteUInt16((long)address, value); + GetMemoryManager().Write(address, value); } public static void WriteUInt32(ulong address, uint value) { - GetMemoryManager().WriteUInt32((long)address, value); + GetMemoryManager().Write(address, value); } public static void WriteUInt64(ulong address, ulong value) { - GetMemoryManager().WriteUInt64((long)address, value); + GetMemoryManager().Write(address, value); } public static void WriteVector128(ulong address, V128 value) { - GetMemoryManager().WriteVector128((long)address, value); + GetMemoryManager().Write(address, value); } -#endregion + #endregion -#region "Write exclusive" + #region "Write exclusive" public static int WriteByteExclusive(ulong address, byte value) { bool success = _context.ExclusiveAddress == GetMaskedExclusiveAddress(address); if (success) { - success = _context.Memory.AtomicCompareExchangeByte( - (long)address, - (byte)_context.ExclusiveValueLow, - (byte)value); + ref int valueRef = ref _context.Memory.GetRefNoChecks(address); + + int currentValue = valueRef; + + byte expected = (byte)_context.ExclusiveValueLow; + + int expected32 = (currentValue & ~byte.MaxValue) | expected; + int desired32 = (currentValue & ~byte.MaxValue) | value; + + success = Interlocked.CompareExchange(ref valueRef, desired32, expected32) == expected32; if (success) { @@ -306,10 +314,16 @@ namespace ARMeilleure.Instructions if (success) { - success = _context.Memory.AtomicCompareExchangeInt16( - (long)address, - (short)_context.ExclusiveValueLow, - (short)value); + ref int valueRef = ref _context.Memory.GetRefNoChecks(address); + + int currentValue = valueRef; + + ushort expected = (ushort)_context.ExclusiveValueLow; + + int expected32 = (currentValue & ~ushort.MaxValue) | expected; + int desired32 = (currentValue & ~ushort.MaxValue) | value; + + success = Interlocked.CompareExchange(ref valueRef, desired32, expected32) == expected32; if (success) { @@ -326,10 +340,9 @@ namespace ARMeilleure.Instructions if (success) { - success = _context.Memory.AtomicCompareExchangeInt32( - (long)address, - (int)_context.ExclusiveValueLow, - (int)value); + ref int valueRef = ref _context.Memory.GetRef(address); + + success = Interlocked.CompareExchange(ref valueRef, (int)value, (int)_context.ExclusiveValueLow) == (int)_context.ExclusiveValueLow; if (success) { @@ -346,10 +359,9 @@ namespace ARMeilleure.Instructions if (success) { - success = _context.Memory.AtomicCompareExchangeInt64( - (long)address, - (long)_context.ExclusiveValueLow, - (long)value); + ref long valueRef = ref _context.Memory.GetRef(address); + + success = Interlocked.CompareExchange(ref valueRef, (long)value, (long)_context.ExclusiveValueLow) == (long)_context.ExclusiveValueLow; if (success) { @@ -368,7 +380,9 @@ namespace ARMeilleure.Instructions { V128 expected = new V128(_context.ExclusiveValueLow, _context.ExclusiveValueHigh); - success = _context.Memory.AtomicCompareExchangeInt128((long)address, expected, value); + ref V128 location = ref _context.Memory.GetRef(address); + + success = MemoryManagerPal.CompareAndSwap128(ref location, expected, value) == expected; if (success) { @@ -378,7 +392,7 @@ namespace ARMeilleure.Instructions return success ? 0 : 1; } -#endregion + #endregion private static ulong GetMaskedExclusiveAddress(ulong address) { @@ -412,7 +426,8 @@ namespace ARMeilleure.Instructions { Statistics.PauseTimer(); - ExecutionContext context = GetContext(); + var context = GetContext(); + context.CheckInterrupt(); Statistics.ResumeTimer(); @@ -420,12 +435,12 @@ namespace ARMeilleure.Instructions return context.Running; } - public static ExecutionContext GetContext() + public static State.ExecutionContext GetContext() { return _context.Context; } - public static MemoryManager GetMemoryManager() + public static IMemoryManager GetMemoryManager() { return _context.Memory; } diff --git a/ARMeilleure/Memory/IJitMemoryAllocator.cs b/ARMeilleure/Memory/IJitMemoryAllocator.cs new file mode 100644 index 00000000..5745a4bf --- /dev/null +++ b/ARMeilleure/Memory/IJitMemoryAllocator.cs @@ -0,0 +1,8 @@ +namespace ARMeilleure.Memory +{ + public interface IJitMemoryAllocator + { + IJitMemoryBlock Allocate(ulong size); + IJitMemoryBlock Reserve(ulong size); + } +} diff --git a/ARMeilleure/Memory/IJitMemoryBlock.cs b/ARMeilleure/Memory/IJitMemoryBlock.cs new file mode 100644 index 00000000..670f2862 --- /dev/null +++ b/ARMeilleure/Memory/IJitMemoryBlock.cs @@ -0,0 +1,14 @@ +using System; + +namespace ARMeilleure.Memory +{ + public interface IJitMemoryBlock : IDisposable + { + IntPtr Pointer { get; } + + bool Commit(ulong offset, ulong size); + + void MapAsRx(ulong offset, ulong size); + void MapAsRwx(ulong offset, ulong size); + } +} diff --git a/ARMeilleure/Memory/IMemoryManager.cs b/ARMeilleure/Memory/IMemoryManager.cs new file mode 100644 index 00000000..ce1f5848 --- /dev/null +++ b/ARMeilleure/Memory/IMemoryManager.cs @@ -0,0 +1,19 @@ +using System; + +namespace ARMeilleure.Memory +{ + public interface IMemoryManager + { + int AddressSpaceBits { get; } + + IntPtr PageTablePointer { get; } + + T Read(ulong va) where T : unmanaged; + void Write(ulong va, T value) where T : unmanaged; + + ref T GetRef(ulong va) where T : unmanaged; + ref T GetRefNoChecks(ulong va) where T : unmanaged; + + bool IsMapped(ulong va); + } +} \ No newline at end of file diff --git a/ARMeilleure/Memory/MemoryManagementUnix.cs b/ARMeilleure/Memory/MemoryManagementUnix.cs deleted file mode 100644 index e9b29608..00000000 --- a/ARMeilleure/Memory/MemoryManagementUnix.cs +++ /dev/null @@ -1,94 +0,0 @@ -using Mono.Unix.Native; -using System; - -namespace ARMeilleure.Memory -{ - static class MemoryManagementUnix - { - public static IntPtr Allocate(ulong size) - { - ulong pageSize = (ulong)Syscall.sysconf(SysconfName._SC_PAGESIZE); - - const MmapProts prot = MmapProts.PROT_READ | MmapProts.PROT_WRITE; - - const MmapFlags flags = MmapFlags.MAP_PRIVATE | MmapFlags.MAP_ANONYMOUS; - - IntPtr ptr = Syscall.mmap(IntPtr.Zero, size + pageSize, prot, flags, -1, 0); - - if (ptr == IntPtr.Zero) - { - throw new OutOfMemoryException(); - } - - unsafe - { - ptr = new IntPtr(ptr.ToInt64() + (long)pageSize); - - *((ulong*)ptr - 1) = size; - } - - return ptr; - } - - public static bool Commit(IntPtr address, ulong size) - { - return Syscall.mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) == 0; - } - - public static bool Reprotect(IntPtr address, ulong size, Memory.MemoryProtection protection) - { - MmapProts prot = GetProtection(protection); - - return Syscall.mprotect(address, size, prot) == 0; - } - - public static IntPtr Reserve(ulong size) - { - ulong pageSize = (ulong)Syscall.sysconf(SysconfName._SC_PAGESIZE); - - const MmapProts prot = MmapProts.PROT_NONE; - - const MmapFlags flags = MmapFlags.MAP_PRIVATE | MmapFlags.MAP_ANONYMOUS; - - IntPtr ptr = Syscall.mmap(IntPtr.Zero, size + pageSize, prot, flags, -1, 0); - - if (ptr == IntPtr.Zero) - { - throw new OutOfMemoryException(); - } - - return ptr; - } - - private static MmapProts GetProtection(Memory.MemoryProtection protection) - { - switch (protection) - { - case Memory.MemoryProtection.None: return MmapProts.PROT_NONE; - case Memory.MemoryProtection.Read: return MmapProts.PROT_READ; - case Memory.MemoryProtection.ReadAndWrite: return MmapProts.PROT_READ | MmapProts.PROT_WRITE; - case Memory.MemoryProtection.ReadAndExecute: return MmapProts.PROT_READ | MmapProts.PROT_EXEC; - case Memory.MemoryProtection.ReadWriteExecute: return MmapProts.PROT_READ | MmapProts.PROT_WRITE | MmapProts.PROT_EXEC; - case Memory.MemoryProtection.Execute: return MmapProts.PROT_EXEC; - - default: throw new ArgumentException($"Invalid permission \"{protection}\"."); - } - } - - public static bool Free(IntPtr address) - { - ulong pageSize = (ulong)Syscall.sysconf(SysconfName._SC_PAGESIZE); - - ulong size; - - unsafe - { - size = *((ulong*)address - 1); - - address = new IntPtr(address.ToInt64() - (long)pageSize); - } - - return Syscall.munmap(address, size + pageSize) == 0; - } - } -} \ No newline at end of file diff --git a/ARMeilleure/Memory/MemoryManagementWindows.cs b/ARMeilleure/Memory/MemoryManagementWindows.cs deleted file mode 100644 index a9455063..00000000 --- a/ARMeilleure/Memory/MemoryManagementWindows.cs +++ /dev/null @@ -1,142 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace ARMeilleure.Memory -{ - static class MemoryManagementWindows - { - [Flags] - private enum AllocationType : uint - { - Commit = 0x1000, - Reserve = 0x2000, - Decommit = 0x4000, - Release = 0x8000, - Reset = 0x80000, - Physical = 0x400000, - TopDown = 0x100000, - WriteWatch = 0x200000, - LargePages = 0x20000000 - } - - [Flags] - private enum MemoryProtection : uint - { - NoAccess = 0x01, - ReadOnly = 0x02, - ReadWrite = 0x04, - WriteCopy = 0x08, - Execute = 0x10, - ExecuteRead = 0x20, - ExecuteReadWrite = 0x40, - ExecuteWriteCopy = 0x80, - GuardModifierflag = 0x100, - NoCacheModifierflag = 0x200, - WriteCombineModifierflag = 0x400 - } - - [DllImport("kernel32.dll")] - private static extern IntPtr VirtualAlloc( - IntPtr lpAddress, - IntPtr dwSize, - AllocationType flAllocationType, - MemoryProtection flProtect); - - [DllImport("kernel32.dll")] - private static extern bool VirtualProtect( - IntPtr lpAddress, - IntPtr dwSize, - MemoryProtection flNewProtect, - out MemoryProtection lpflOldProtect); - - [DllImport("kernel32.dll")] - private static extern bool VirtualFree( - IntPtr lpAddress, - IntPtr dwSize, - AllocationType dwFreeType); - - public static IntPtr Allocate(IntPtr size) - { - const AllocationType flags = - AllocationType.Reserve | - AllocationType.Commit; - - IntPtr ptr = VirtualAlloc(IntPtr.Zero, size, flags, MemoryProtection.ReadWrite); - - if (ptr == IntPtr.Zero) - { - throw new OutOfMemoryException(); - } - - return ptr; - } - - public static IntPtr AllocateWriteTracked(IntPtr size) - { - const AllocationType flags = - AllocationType.Reserve | - AllocationType.Commit | - AllocationType.WriteWatch; - - IntPtr ptr = VirtualAlloc(IntPtr.Zero, size, flags, MemoryProtection.ReadWrite); - - if (ptr == IntPtr.Zero) - { - throw new OutOfMemoryException(); - } - - return ptr; - } - - public static bool Commit(IntPtr location, IntPtr size) - { - const AllocationType flags = AllocationType.Commit; - - IntPtr ptr = VirtualAlloc(location, size, flags, MemoryProtection.ReadWrite); - - return ptr != IntPtr.Zero; - } - - public static bool Reprotect(IntPtr address, IntPtr size, Memory.MemoryProtection protection) - { - MemoryProtection prot = GetProtection(protection); - - return VirtualProtect(address, size, prot, out _); - } - - public static IntPtr Reserve(IntPtr size) - { - const AllocationType flags = AllocationType.Reserve; - - IntPtr ptr = VirtualAlloc(IntPtr.Zero, size, flags, MemoryProtection.ReadWrite); - - if (ptr == IntPtr.Zero) - { - throw new OutOfMemoryException(); - } - - return ptr; - } - - private static MemoryProtection GetProtection(Memory.MemoryProtection protection) - { - switch (protection) - { - case Memory.MemoryProtection.None: return MemoryProtection.NoAccess; - case Memory.MemoryProtection.Read: return MemoryProtection.ReadOnly; - case Memory.MemoryProtection.ReadAndWrite: return MemoryProtection.ReadWrite; - case Memory.MemoryProtection.ReadAndExecute: return MemoryProtection.ExecuteRead; - case Memory.MemoryProtection.ReadWriteExecute: return MemoryProtection.ExecuteReadWrite; - case Memory.MemoryProtection.Execute: return MemoryProtection.Execute; - - default: throw new ArgumentException($"Invalid permission \"{protection}\"."); - } - } - - public static bool Free(IntPtr address) - { - return VirtualFree(address, IntPtr.Zero, AllocationType.Release); - } - } -} \ No newline at end of file diff --git a/ARMeilleure/Memory/MemoryManager.cs b/ARMeilleure/Memory/MemoryManager.cs deleted file mode 100644 index c5a0323b..00000000 --- a/ARMeilleure/Memory/MemoryManager.cs +++ /dev/null @@ -1,738 +0,0 @@ -using ARMeilleure.State; -using System; -using System.Collections.Generic; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Threading; - -using static ARMeilleure.Memory.MemoryManagement; - -namespace ARMeilleure.Memory -{ - public unsafe class MemoryManager - { - public const int PageBits = 12; - public const int PageSize = 1 << PageBits; - public const int PageMask = PageSize - 1; - - internal const long PteFlagsMask = 7; - - public IntPtr Ram { get; private set; } - - private byte* _ramPtr; - - private IntPtr _pageTable; - - internal IntPtr PageTable => _pageTable; - - internal int PtLevelBits { get; } - internal int PtLevelSize { get; } - internal int PtLevelMask { get; } - - public int AddressSpaceBits { get; } - public long AddressSpaceSize { get; } - - public MemoryManager( - IntPtr ram, - int addressSpaceBits = 48, - bool useFlatPageTable = false) - { - Ram = ram; - - _ramPtr = (byte*)ram; - - AddressSpaceBits = addressSpaceBits; - AddressSpaceSize = 1L << addressSpaceBits; - - // When flat page table is requested, we use a single - // array for the mappings of the entire address space. - // This has better performance, but also high memory usage. - // The multi level page table uses 9 bits per level, so - // the memory usage is lower, but the performance is also - // lower, since each address translation requires multiple reads. - if (useFlatPageTable) - { - PtLevelBits = addressSpaceBits - PageBits; - } - else - { - PtLevelBits = 9; - } - - PtLevelSize = 1 << PtLevelBits; - PtLevelMask = PtLevelSize - 1; - - _pageTable = Allocate((ulong)(PtLevelSize * IntPtr.Size)); - } - - public void Map(long va, long pa, long size) - { - SetPtEntries(va, _ramPtr + pa, size); - } - - public void Unmap(long position, long size) - { - SetPtEntries(position, null, size); - } - - public bool IsMapped(long position) - { - return Translate(position) != IntPtr.Zero; - } - - public long GetPhysicalAddress(long virtualAddress) - { - byte* ptr = (byte*)Translate(virtualAddress); - - return (long)(ptr - _ramPtr); - } - - private IntPtr Translate(long position) - { - if (!IsValidPosition(position)) - { - return IntPtr.Zero; - } - - byte* ptr = GetPtEntry(position); - - ulong ptrUlong = (ulong)ptr; - - if ((ptrUlong & PteFlagsMask) != 0) - { - ptrUlong &= ~(ulong)PteFlagsMask; - - ptr = (byte*)ptrUlong; - } - - if (ptr == null) - { - return IntPtr.Zero; - } - - return new IntPtr(ptr + (position & PageMask)); - } - - private IntPtr TranslateWrite(long position) - { - if (!IsValidPosition(position)) - { - return IntPtr.Zero; - } - - byte* ptr = GetPtEntry(position); - - ulong ptrUlong = (ulong)ptr; - - if ((ptrUlong & PteFlagsMask) != 0) - { - ClearPtEntryFlag(position, PteFlagsMask); - - ptrUlong &= ~(ulong)PteFlagsMask; - - ptr = (byte*)ptrUlong; - } - - return new IntPtr(ptr + (position & PageMask)); - } - - private byte* GetPtEntry(long position) - { - return *(byte**)GetPtPtr(position); - } - - private void SetPtEntries(long va, byte* ptr, long size) - { - long endPosition = (va + size + PageMask) & ~PageMask; - - while ((ulong)va < (ulong)endPosition) - { - SetPtEntry(va, ptr); - - va += PageSize; - - if (ptr != null) - { - ptr += PageSize; - } - } - } - - private void SetPtEntry(long position, byte* ptr) - { - *(byte**)GetPtPtr(position) = ptr; - } - - private void SetPtEntryFlag(long position, long flag) - { - ModifyPtEntryFlag(position, flag, setFlag: true); - } - - private void ClearPtEntryFlag(long position, long flag) - { - ModifyPtEntryFlag(position, flag, setFlag: false); - } - - private void ModifyPtEntryFlag(long position, long flag, bool setFlag) - { - IntPtr* pt = (IntPtr*)_pageTable; - - while (true) - { - IntPtr* ptPtr = GetPtPtr(position); - - IntPtr old = *ptPtr; - - long modified = old.ToInt64(); - - if (setFlag) - { - modified |= flag; - } - else - { - modified &= ~flag; - } - - IntPtr origValue = Interlocked.CompareExchange(ref *ptPtr, new IntPtr(modified), old); - - if (origValue == old) - { - break; - } - } - } - - private IntPtr* GetPtPtr(long position) - { - if (!IsValidPosition(position)) - { - throw new ArgumentOutOfRangeException(nameof(position)); - } - - IntPtr nextPtr = _pageTable; - - IntPtr* ptePtr = null; - - int bit = PageBits; - - while (true) - { - long index = (position >> bit) & PtLevelMask; - - ptePtr = &((IntPtr*)nextPtr)[index]; - - bit += PtLevelBits; - - if (bit >= AddressSpaceBits) - { - break; - } - - nextPtr = *ptePtr; - - if (nextPtr == IntPtr.Zero) - { - // Entry does not yet exist, allocate a new one. - IntPtr newPtr = Allocate((ulong)(PtLevelSize * IntPtr.Size)); - - // Try to swap the current pointer (should be zero), with the allocated one. - nextPtr = Interlocked.CompareExchange(ref *ptePtr, newPtr, IntPtr.Zero); - - // If the old pointer is not null, then another thread already has set it. - if (nextPtr != IntPtr.Zero) - { - Free(newPtr); - } - else - { - nextPtr = newPtr; - } - } - } - - return ptePtr; - } - - public unsafe (ulong, ulong)[] GetModifiedRanges(ulong address, ulong size, int id) - { - ulong idMask = 1UL << id; - - List<(ulong, ulong)> ranges = new List<(ulong, ulong)>(); - - ulong endAddress = (address + size + PageMask) & ~(ulong)PageMask; - - address &= ~(ulong)PageMask; - - ulong currAddr = address; - ulong currSize = 0; - - while (address < endAddress) - { - // If the address is invalid, we stop and consider all the remaining memory - // as not modified (since the address is invalid, we can't check, and technically - // the memory doesn't exist). - if (!IsValidPosition((long)address)) - { - break; - } - - byte* ptr = ((byte**)_pageTable)[address >> PageBits]; - - ulong ptrUlong = (ulong)ptr; - - if ((ptrUlong & idMask) == 0) - { - // Modified. - currSize += PageSize; - - SetPtEntryFlag((long)address, (long)idMask); - } - else - { - if (currSize != 0) - { - ranges.Add((currAddr, currSize)); - } - - currAddr = address + PageSize; - currSize = 0; - } - - address += PageSize; - } - - if (currSize != 0) - { - ranges.Add((currAddr, currSize)); - } - - return ranges.ToArray(); - } - - private bool IsContiguous(long position, long size) - { - long endPos = position + size; - - position &= ~PageMask; - - long expectedPa = GetPhysicalAddress(position); - - while ((ulong)position < (ulong)endPos) - { - long pa = GetPhysicalAddress(position); - - if (pa != expectedPa) - { - return false; - } - - position += PageSize; - expectedPa += PageSize; - } - - return true; - } - - public bool IsValidPosition(long position) - { - return (ulong)position < (ulong)AddressSpaceSize; - } - - internal V128 AtomicLoadInt128(long position) - { - if ((position & 0xf) != 0) - { - AbortWithAlignmentFault(position); - } - - IntPtr ptr = TranslateWrite(position); - - return MemoryManagerPal.AtomicLoad128(ptr); - } - - internal bool AtomicCompareExchangeByte(long position, byte expected, byte desired) - { - int* ptr = (int*)Translate(position); - - int currentValue = *ptr; - - int expected32 = (currentValue & ~byte.MaxValue) | expected; - int desired32 = (currentValue & ~byte.MaxValue) | desired; - - return Interlocked.CompareExchange(ref *ptr, desired32, expected32) == expected32; - } - - internal bool AtomicCompareExchangeInt16(long position, short expected, short desired) - { - if ((position & 1) != 0) - { - AbortWithAlignmentFault(position); - } - - int* ptr = (int*)Translate(position); - - int currentValue = *ptr; - - int expected32 = (currentValue & ~ushort.MaxValue) | (ushort)expected; - int desired32 = (currentValue & ~ushort.MaxValue) | (ushort)desired; - - return Interlocked.CompareExchange(ref *ptr, desired32, expected32) == expected32; - } - - public bool AtomicCompareExchangeInt32(long position, int expected, int desired) - { - if ((position & 3) != 0) - { - AbortWithAlignmentFault(position); - } - - int* ptr = (int*)TranslateWrite(position); - - return Interlocked.CompareExchange(ref *ptr, desired, expected) == expected; - } - - internal bool AtomicCompareExchangeInt64(long position, long expected, long desired) - { - if ((position & 7) != 0) - { - AbortWithAlignmentFault(position); - } - - long* ptr = (long*)TranslateWrite(position); - - return Interlocked.CompareExchange(ref *ptr, desired, expected) == expected; - } - - internal bool AtomicCompareExchangeInt128(long position, V128 expected, V128 desired) - { - if ((position & 0xf) != 0) - { - AbortWithAlignmentFault(position); - } - - IntPtr ptr = TranslateWrite(position); - - return MemoryManagerPal.CompareAndSwap128(ptr, expected, desired) == expected; - } - - public int AtomicIncrementInt32(long position) - { - if ((position & 3) != 0) - { - AbortWithAlignmentFault(position); - } - - int* ptr = (int*)TranslateWrite(position); - - return Interlocked.Increment(ref *ptr); - } - - public int AtomicDecrementInt32(long position) - { - if ((position & 3) != 0) - { - AbortWithAlignmentFault(position); - } - - int* ptr = (int*)TranslateWrite(position); - - return Interlocked.Decrement(ref *ptr); - } - - private void AbortWithAlignmentFault(long position) - { - // TODO: Abort mode and exception support on the CPU. - throw new InvalidOperationException($"Tried to compare exchange a misaligned address 0x{position:X16}."); - } - - public sbyte ReadSByte(long position) - { - return (sbyte)ReadByte(position); - } - - public short ReadInt16(long position) - { - return (short)ReadUInt16(position); - } - - public int ReadInt32(long position) - { - return (int)ReadUInt32(position); - } - - public long ReadInt64(long position) - { - return (long)ReadUInt64(position); - } - - public byte ReadByte(long position) - { - return *((byte*)Translate(position)); - } - - public ushort ReadUInt16(long position) - { - if ((position & 1) == 0) - { - return *((ushort*)Translate(position)); - } - else - { - return (ushort)(ReadByte(position + 0) << 0 | - ReadByte(position + 1) << 8); - } - } - - public uint ReadUInt32(long position) - { - if ((position & 3) == 0) - { - return *((uint*)Translate(position)); - } - else - { - return (uint)(ReadUInt16(position + 0) << 0 | - ReadUInt16(position + 2) << 16); - } - } - - public ulong ReadUInt64(long position) - { - if ((position & 7) == 0) - { - return *((ulong*)Translate(position)); - } - else - { - return (ulong)ReadUInt32(position + 0) << 0 | - (ulong)ReadUInt32(position + 4) << 32; - } - } - - public V128 ReadVector128(long position) - { - return new V128(ReadUInt64(position), ReadUInt64(position + 8)); - } - - public byte[] ReadBytes(long position, long size) - { - long endAddr = position + size; - - if ((ulong)size > int.MaxValue) - { - throw new ArgumentOutOfRangeException(nameof(size)); - } - - if ((ulong)endAddr < (ulong)position) - { - throw new ArgumentOutOfRangeException(nameof(position)); - } - - byte[] data = new byte[size]; - - int offset = 0; - - while ((ulong)position < (ulong)endAddr) - { - long pageLimit = (position + PageSize) & ~(long)PageMask; - - if ((ulong)pageLimit > (ulong)endAddr) - { - pageLimit = endAddr; - } - - int copySize = (int)(pageLimit - position); - - Marshal.Copy(Translate(position), data, offset, copySize); - - position += copySize; - offset += copySize; - } - - return data; - } - - public ReadOnlySpan GetSpan(ulong address, ulong size) - { - if (IsContiguous(address, size)) - { - return new ReadOnlySpan((void*)Translate((long)address), (int)size); - } - else - { - return ReadBytes((long)address, (long)size); - } - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private bool IsContiguous(ulong address, ulong size) - { - if (!IsValidPosition((long)address)) - { - return false; - } - - ulong endVa = (address + size + PageMask) & ~(ulong)PageMask; - - address &= ~(ulong)PageMask; - - int pages = (int)((endVa - address) / PageSize); - - for (int page = 0; page < pages - 1; page++) - { - if (!IsValidPosition((long)address + PageSize)) - { - return false; - } - - if (GetPtEntry((long)address) + PageSize != GetPtEntry((long)address + PageSize)) - { - return false; - } - - address += PageSize; - } - - return true; - } - - public void WriteSByte(long position, sbyte value) - { - WriteByte(position, (byte)value); - } - - public void WriteInt16(long position, short value) - { - WriteUInt16(position, (ushort)value); - } - - public void WriteInt32(long position, int value) - { - WriteUInt32(position, (uint)value); - } - - public void WriteInt64(long position, long value) - { - WriteUInt64(position, (ulong)value); - } - - public void WriteByte(long position, byte value) - { - *((byte*)TranslateWrite(position)) = value; - } - - public void WriteUInt16(long position, ushort value) - { - if ((position & 1) == 0) - { - *((ushort*)TranslateWrite(position)) = value; - } - else - { - WriteByte(position + 0, (byte)(value >> 0)); - WriteByte(position + 1, (byte)(value >> 8)); - } - } - - public void WriteUInt32(long position, uint value) - { - if ((position & 3) == 0) - { - *((uint*)TranslateWrite(position)) = value; - } - else - { - WriteUInt16(position + 0, (ushort)(value >> 0)); - WriteUInt16(position + 2, (ushort)(value >> 16)); - } - } - - public void WriteUInt64(long position, ulong value) - { - if ((position & 7) == 0) - { - *((ulong*)TranslateWrite(position)) = value; - } - else - { - WriteUInt32(position + 0, (uint)(value >> 0)); - WriteUInt32(position + 4, (uint)(value >> 32)); - } - } - - public void WriteVector128(long position, V128 value) - { - WriteUInt64(position + 0, value.Extract(0)); - WriteUInt64(position + 8, value.Extract(1)); - } - - public void WriteBytes(long position, byte[] data) - { - long endAddr = position + data.Length; - - if ((ulong)endAddr < (ulong)position) - { - throw new ArgumentOutOfRangeException(nameof(position)); - } - - int offset = 0; - - while ((ulong)position < (ulong)endAddr) - { - long pageLimit = (position + PageSize) & ~(long)PageMask; - - if ((ulong)pageLimit > (ulong)endAddr) - { - pageLimit = endAddr; - } - - int copySize = (int)(pageLimit - position); - - Marshal.Copy(data, offset, TranslateWrite(position), copySize); - - position += copySize; - offset += copySize; - } - } - - public void Dispose() - { - Dispose(true); - } - - protected virtual void Dispose(bool disposing) - { - IntPtr ptr = Interlocked.Exchange(ref _pageTable, IntPtr.Zero); - - if (ptr != IntPtr.Zero) - { - FreePageTableEntry(ptr, PageBits); - } - } - - private void FreePageTableEntry(IntPtr ptr, int levelBitEnd) - { - levelBitEnd += PtLevelBits; - - if (levelBitEnd >= AddressSpaceBits) - { - Free(ptr); - - return; - } - - for (int index = 0; index < PtLevelSize; index++) - { - IntPtr ptePtr = ((IntPtr*)ptr)[index]; - - if (ptePtr != IntPtr.Zero) - { - FreePageTableEntry(ptePtr, levelBitEnd); - } - } - - Free(ptr); - } - } -} \ No newline at end of file diff --git a/ARMeilleure/Memory/MemoryManagerPal.cs b/ARMeilleure/Memory/MemoryManagerPal.cs index 66c43642..0dc83959 100644 --- a/ARMeilleure/Memory/MemoryManagerPal.cs +++ b/ARMeilleure/Memory/MemoryManagerPal.cs @@ -1,13 +1,12 @@ using ARMeilleure.IntermediateRepresentation; using ARMeilleure.State; using ARMeilleure.Translation; -using System; namespace ARMeilleure.Memory { static class MemoryManagerPal { - private delegate V128 CompareExchange128(IntPtr address, V128 expected, V128 desired); + private delegate V128 CompareExchange128(ref V128 location, V128 expected, V128 desired); private static CompareExchange128 _compareExchange128; @@ -18,14 +17,14 @@ namespace ARMeilleure.Memory _lock = new object(); } - public static V128 AtomicLoad128(IntPtr address) + public static V128 AtomicLoad128(ref V128 location) { - return GetCompareAndSwap128()(address, V128.Zero, V128.Zero); + return GetCompareAndSwap128()(ref location, V128.Zero, V128.Zero); } - public static V128 CompareAndSwap128(IntPtr address, V128 expected, V128 desired) + public static V128 CompareAndSwap128(ref V128 location, V128 expected, V128 desired) { - return GetCompareAndSwap128()(address, expected, desired); + return GetCompareAndSwap128()(ref location, expected, desired); } private static CompareExchange128 GetCompareAndSwap128() diff --git a/ARMeilleure/Memory/MemoryProtection.cs b/ARMeilleure/Memory/MemoryProtection.cs deleted file mode 100644 index 6bc16f8e..00000000 --- a/ARMeilleure/Memory/MemoryProtection.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; - -namespace ARMeilleure.Memory -{ - [Flags] - public enum MemoryProtection - { - None = 0, - Read = 1 << 0, - Write = 1 << 1, - Execute = 1 << 2, - - ReadAndWrite = Read | Write, - ReadAndExecute = Read | Execute, - ReadWriteExecute = Read | Write | Execute - } -} \ No newline at end of file diff --git a/ARMeilleure/Memory/MemoryProtectionException.cs b/ARMeilleure/Memory/MemoryProtectionException.cs deleted file mode 100644 index 6313ce6a..00000000 --- a/ARMeilleure/Memory/MemoryProtectionException.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace ARMeilleure.Memory -{ - class MemoryProtectionException : Exception - { - public MemoryProtectionException(MemoryProtection protection) : base($"Failed to set memory protection to \"{protection}\".") { } - } -} \ No newline at end of file diff --git a/ARMeilleure/Memory/ReservedRegion.cs b/ARMeilleure/Memory/ReservedRegion.cs index 521019ad..dc6eb9da 100644 --- a/ARMeilleure/Memory/ReservedRegion.cs +++ b/ARMeilleure/Memory/ReservedRegion.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Text; namespace ARMeilleure.Memory { @@ -8,20 +6,22 @@ namespace ARMeilleure.Memory { private const int DefaultGranularity = 65536; // Mapping granularity in Windows. - public IntPtr Pointer { get; } + public IJitMemoryBlock Block { get; } - private ulong _maxSize; - private ulong _sizeGranularity; + public IntPtr Pointer => Block.Pointer; + + private readonly ulong _maxSize; + private readonly ulong _sizeGranularity; private ulong _currentSize; - public ReservedRegion(ulong maxSize, ulong granularity = 0) + public ReservedRegion(IJitMemoryAllocator allocator, ulong maxSize, ulong granularity = 0) { if (granularity == 0) { granularity = DefaultGranularity; } - Pointer = MemoryManagement.Reserve(maxSize); + Block = allocator.Reserve(maxSize); _maxSize = maxSize; _sizeGranularity = granularity; _currentSize = 0; @@ -43,7 +43,7 @@ namespace ARMeilleure.Memory { ulong overflowBytes = desiredSize - _currentSize; ulong moreToCommit = (((_sizeGranularity - 1) + overflowBytes) / _sizeGranularity) * _sizeGranularity; // Round up. - MemoryManagement.Commit(new IntPtr((long)Pointer + (long)_currentSize), moreToCommit); + Block.Commit(_currentSize, moreToCommit); _currentSize += moreToCommit; } } diff --git a/ARMeilleure/State/ExecutionContext.cs b/ARMeilleure/State/ExecutionContext.cs index 57a05dbf..866eafbc 100644 --- a/ARMeilleure/State/ExecutionContext.cs +++ b/ARMeilleure/State/ExecutionContext.cs @@ -1,3 +1,4 @@ +using ARMeilleure.Memory; using System; using System.Diagnostics; @@ -73,9 +74,9 @@ namespace ARMeilleure.State _tickCounter.Start(); } - public ExecutionContext() + public ExecutionContext(IJitMemoryAllocator allocator) { - _nativeContext = new NativeContext(); + _nativeContext = new NativeContext(allocator); Running = true; diff --git a/ARMeilleure/State/NativeContext.cs b/ARMeilleure/State/NativeContext.cs index 95d976ee..10c4c37f 100644 --- a/ARMeilleure/State/NativeContext.cs +++ b/ARMeilleure/State/NativeContext.cs @@ -17,11 +17,13 @@ namespace ARMeilleure.State RegisterConsts.FlagsCount * FlagSize + RegisterConsts.FpFlagsCount * FlagSize + ExtraSize; - public IntPtr BasePtr { get; } + private readonly IJitMemoryBlock _block; - public NativeContext() + public IntPtr BasePtr => _block.Pointer; + + public NativeContext(IJitMemoryAllocator allocator) { - BasePtr = MemoryManagement.Allocate(TotalSize); + _block = allocator.Allocate(TotalSize); } public ulong GetX(int index) @@ -193,7 +195,7 @@ namespace ARMeilleure.State public void Dispose() { - MemoryManagement.Free(BasePtr); + _block.Dispose(); } } } \ No newline at end of file diff --git a/ARMeilleure/Translation/ArmEmitterContext.cs b/ARMeilleure/Translation/ArmEmitterContext.cs index d1a2c92d..a905c722 100644 --- a/ARMeilleure/Translation/ArmEmitterContext.cs +++ b/ARMeilleure/Translation/ArmEmitterContext.cs @@ -37,7 +37,7 @@ namespace ARMeilleure.Translation public OpCode CurrOp { get; set; } - public MemoryManager Memory { get; } + public IMemoryManager Memory { get; } public Aarch32Mode Mode { get; } @@ -47,7 +47,7 @@ namespace ARMeilleure.Translation public bool HighCq { get; } - public ArmEmitterContext(MemoryManager memory, JumpTable jumpTable, long baseAddress, bool highCq, Aarch32Mode mode) + public ArmEmitterContext(IMemoryManager memory, JumpTable jumpTable, long baseAddress, bool highCq, Aarch32Mode mode) { Memory = memory; JumpTable = jumpTable; diff --git a/ARMeilleure/Translation/Compiler.cs b/ARMeilleure/Translation/Compiler.cs index d27c3cce..ec2f2968 100644 --- a/ARMeilleure/Translation/Compiler.cs +++ b/ARMeilleure/Translation/Compiler.cs @@ -11,14 +11,14 @@ namespace ARMeilleure.Translation { public static T Compile(ControlFlowGraph cfg, OperandType[] argTypes, OperandType retType, CompilerOptions options) { - CompiledFunction func = CompileAndGetCf(cfg, argTypes, retType, options); + CompiledFunction func = Compile(cfg, argTypes, retType, options); IntPtr codePtr = JitCache.Map(func); return Marshal.GetDelegateForFunctionPointer(codePtr); } - public static CompiledFunction CompileAndGetCf(ControlFlowGraph cfg, OperandType[] argTypes, OperandType retType, CompilerOptions options) + public static CompiledFunction Compile(ControlFlowGraph cfg, OperandType[] argTypes, OperandType retType, CompilerOptions options) { Logger.StartPass(PassName.Dominance); diff --git a/ARMeilleure/Translation/DirectCallStubs.cs b/ARMeilleure/Translation/DirectCallStubs.cs index e6e87b2b..42a78c71 100644 --- a/ARMeilleure/Translation/DirectCallStubs.cs +++ b/ARMeilleure/Translation/DirectCallStubs.cs @@ -17,14 +17,9 @@ namespace ARMeilleure.Translation private static GuestFunction _indirectCallStub; private static GuestFunction _indirectTailCallStub; - private static object _lock; + private static readonly object _lock = new object(); private static bool _initialized; - static DirectCallStubs() - { - _lock = new object(); - } - public static void InitializeStubs() { if (_initialized) return; @@ -85,11 +80,7 @@ namespace ARMeilleure.Translation OperandType.I64 }; - return Compiler.Compile( - cfg, - argTypes, - OperandType.I64, - CompilerOptions.HighCq); + return Compiler.Compile(cfg, argTypes, OperandType.I64, CompilerOptions.HighCq); } /// @@ -121,11 +112,7 @@ namespace ARMeilleure.Translation OperandType.I64 }; - return Compiler.Compile( - cfg, - argTypes, - OperandType.I64, - CompilerOptions.HighCq); + return Compiler.Compile(cfg, argTypes, OperandType.I64, CompilerOptions.HighCq); } } } diff --git a/ARMeilleure/Translation/JitCache.cs b/ARMeilleure/Translation/JitCache.cs index b004cc22..32d40c20 100644 --- a/ARMeilleure/Translation/JitCache.cs +++ b/ARMeilleure/Translation/JitCache.cs @@ -2,6 +2,7 @@ using ARMeilleure.CodeGen; using ARMeilleure.Memory; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Runtime.InteropServices; namespace ARMeilleure.Translation @@ -12,35 +13,33 @@ namespace ARMeilleure.Translation private const int PageMask = PageSize - 1; private const int CodeAlignment = 4; // Bytes - private const int CacheSize = 2047 * 1024 * 1024; private static ReservedRegion _jitRegion; - - private static IntPtr _basePointer => _jitRegion.Pointer; - private static int _offset; + private static readonly List _cacheEntries = new List(); - private static List _cacheEntries; + private static readonly object _lock = new object(); + private static bool _initialized; - private static object _lock; - - static JitCache() + public static void Initialize(IJitMemoryAllocator allocator) { - _jitRegion = new ReservedRegion(CacheSize); - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (_initialized) return; + lock (_lock) { - _jitRegion.ExpandIfNeeded(PageSize); - JitUnwindWindows.InstallFunctionTableHandler(_basePointer, CacheSize); + if (_initialized) return; + _jitRegion = new ReservedRegion(allocator, CacheSize); - // The first page is used for the table based SEH structs. - _offset = PageSize; + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + _jitRegion.ExpandIfNeeded(PageSize); + JitUnwindWindows.InstallFunctionTableHandler(_jitRegion.Pointer, CacheSize); + + // The first page is used for the table based SEH structs. + _offset = PageSize; + } + _initialized = true; } - - _cacheEntries = new List(); - - _lock = new object(); } public static IntPtr Map(CompiledFunction func) @@ -49,9 +48,11 @@ namespace ARMeilleure.Translation lock (_lock) { + Debug.Assert(_initialized); + int funcOffset = Allocate(code.Length); - IntPtr funcPtr = _basePointer + funcOffset; + IntPtr funcPtr = _jitRegion.Pointer + funcOffset; Marshal.Copy(code, 0, funcPtr, code.Length); @@ -77,18 +78,14 @@ namespace ARMeilleure.Translation if (fullPagesSize != 0) { - IntPtr funcPtr = _basePointer + pageStart; - - MemoryManagement.Reprotect(funcPtr, (ulong)fullPagesSize, MemoryProtection.ReadAndExecute); + _jitRegion.Block.MapAsRx((ulong)pageStart, (ulong)fullPagesSize); } int remaining = endOffs - pageEnd; if (remaining != 0) { - IntPtr funcPtr = _basePointer + pageEnd; - - MemoryManagement.Reprotect(funcPtr, (ulong)remaining, MemoryProtection.ReadWriteExecute); + _jitRegion.Block.MapAsRwx((ulong)pageEnd, (ulong)remaining); } } @@ -132,7 +129,7 @@ namespace ARMeilleure.Translation } } - entry = default(JitCacheEntry); + entry = default; return false; } diff --git a/ARMeilleure/Translation/JumpTable.cs b/ARMeilleure/Translation/JumpTable.cs index 5cad2944..e444e96d 100644 --- a/ARMeilleure/Translation/JumpTable.cs +++ b/ARMeilleure/Translation/JumpTable.cs @@ -9,13 +9,6 @@ namespace ARMeilleure.Translation { class JumpTable { - public static JumpTable Instance { get; } - - static JumpTable() - { - Instance = new JumpTable(); - } - // The jump table is a block of (guestAddress, hostAddress) function mappings. // Each entry corresponds to one branch in a JIT compiled function. The entries are // reserved specifically for each call. @@ -60,23 +53,23 @@ namespace ARMeilleure.Translation public IntPtr JumpPointer => _jumpRegion.Pointer; public IntPtr DynamicPointer => _dynamicRegion.Pointer; - public JumpTable() + public JumpTable(IJitMemoryAllocator allocator) { - _jumpRegion = new ReservedRegion(JumpTableByteSize); - _dynamicRegion = new ReservedRegion(DynamicTableByteSize); + _jumpRegion = new ReservedRegion(allocator, JumpTableByteSize); + _dynamicRegion = new ReservedRegion(allocator, DynamicTableByteSize); _targets = new ConcurrentDictionary(); _dependants = new ConcurrentDictionary>(); } - public void RegisterFunction(ulong address, TranslatedFunction func) { + public void RegisterFunction(ulong address, TranslatedFunction func) + { address &= ~3UL; _targets.AddOrUpdate(address, func, (key, oldFunc) => func); long funcPtr = func.GetPointer().ToInt64(); // Update all jump table entries that target this address. - LinkedList myDependants; - if (_dependants.TryGetValue(address, out myDependants)) + if (_dependants.TryGetValue(address, out LinkedList myDependants)) { lock (myDependants) { @@ -125,8 +118,7 @@ namespace ARMeilleure.Translation // Is the address we have already registered? If so, put the function address in the jump table. // If not, it will point to the direct call stub. long value = (long)DirectCallStubs.DirectCallStub(isJump); - TranslatedFunction func; - if (_targets.TryGetValue((ulong)address, out func)) + if (_targets.TryGetValue((ulong)address, out TranslatedFunction func)) { value = func.GetPointer().ToInt64(); } diff --git a/ARMeilleure/Translation/Translator.cs b/ARMeilleure/Translation/Translator.cs index 053c7328..700b54c2 100644 --- a/ARMeilleure/Translation/Translator.cs +++ b/ARMeilleure/Translation/Translator.cs @@ -9,6 +9,7 @@ using System.Collections.Concurrent; using System.Threading; using static ARMeilleure.IntermediateRepresentation.OperandHelper; +using static ARMeilleure.IntermediateRepresentation.OperationHelper; namespace ARMeilleure.Translation { @@ -18,30 +19,31 @@ namespace ARMeilleure.Translation private const bool AlwaysTranslateFunctions = true; // If false, only translates a single block for lowCq. - private MemoryManager _memory; + private readonly IMemoryManager _memory; - private ConcurrentDictionary _funcs; + private readonly ConcurrentDictionary _funcs; - private JumpTable _jumpTable; + private readonly JumpTable _jumpTable; - private PriorityQueue _backgroundQueue; + private readonly PriorityQueue _backgroundQueue; - private AutoResetEvent _backgroundTranslatorEvent; + private readonly AutoResetEvent _backgroundTranslatorEvent; private volatile int _threadCount; - public Translator(MemoryManager memory) + public Translator(IJitMemoryAllocator allocator, IMemoryManager memory) { _memory = memory; _funcs = new ConcurrentDictionary(); - _jumpTable = JumpTable.Instance; + _jumpTable = new JumpTable(allocator); _backgroundQueue = new PriorityQueue(2); _backgroundTranslatorEvent = new AutoResetEvent(false); + JitCache.Initialize(allocator); DirectCallStubs.InitializeStubs(); } @@ -146,8 +148,8 @@ namespace ARMeilleure.Translation { ArmEmitterContext context = new ArmEmitterContext(_memory, _jumpTable, (long)address, highCq, Aarch32Mode.User); - OperandHelper.PrepareOperandPool(highCq); - OperationHelper.PrepareOperationPool(highCq); + PrepareOperandPool(highCq); + PrepareOperationPool(highCq); Logger.StartPass(PassName.Decoding); @@ -178,14 +180,12 @@ namespace ARMeilleure.Translation OperandType[] argTypes = new OperandType[] { OperandType.I64 }; - CompilerOptions options = highCq - ? CompilerOptions.HighCq - : CompilerOptions.None; + CompilerOptions options = highCq ? CompilerOptions.HighCq : CompilerOptions.None; GuestFunction func = Compiler.Compile(cfg, argTypes, OperandType.I64, options); - OperandHelper.ResetOperandPool(highCq); - OperationHelper.ResetOperationPool(highCq); + ResetOperandPool(highCq); + ResetOperationPool(highCq); return new TranslatedFunction(func, rejit: !highCq); } diff --git a/Ryujinx.Cpu/CpuContext.cs b/Ryujinx.Cpu/CpuContext.cs new file mode 100644 index 00000000..275fcc68 --- /dev/null +++ b/Ryujinx.Cpu/CpuContext.cs @@ -0,0 +1,19 @@ +using ARMeilleure.State; +using ARMeilleure.Translation; + +namespace Ryujinx.Cpu +{ + public class CpuContext + { + private readonly Translator _translator; + + public CpuContext(MemoryManager memory) + { + _translator = new Translator(new JitMemoryAllocator(), memory); + } + + public static ExecutionContext CreateExecutionContext() => new ExecutionContext(new JitMemoryAllocator()); + + public void Execute(ExecutionContext context, ulong address) => _translator.Execute(context, address); + } +} diff --git a/Ryujinx.Cpu/JitMemoryAllocator.cs b/Ryujinx.Cpu/JitMemoryAllocator.cs new file mode 100644 index 00000000..26ccd732 --- /dev/null +++ b/Ryujinx.Cpu/JitMemoryAllocator.cs @@ -0,0 +1,11 @@ +using ARMeilleure.Memory; +using Ryujinx.Memory; + +namespace Ryujinx.Cpu +{ + class JitMemoryAllocator : IJitMemoryAllocator + { + public IJitMemoryBlock Allocate(ulong size) => new JitMemoryBlock(size, MemoryAllocationFlags.None); + public IJitMemoryBlock Reserve(ulong size) => new JitMemoryBlock(size, MemoryAllocationFlags.Reserve); + } +} diff --git a/Ryujinx.Cpu/JitMemoryBlock.cs b/Ryujinx.Cpu/JitMemoryBlock.cs new file mode 100644 index 00000000..3ad62d71 --- /dev/null +++ b/Ryujinx.Cpu/JitMemoryBlock.cs @@ -0,0 +1,24 @@ +using ARMeilleure.Memory; +using Ryujinx.Memory; +using System; + +namespace Ryujinx.Cpu +{ + class JitMemoryBlock : IJitMemoryBlock + { + private readonly MemoryBlock _impl; + + public IntPtr Pointer => _impl.Pointer; + + public JitMemoryBlock(ulong size, MemoryAllocationFlags flags) + { + _impl = new MemoryBlock(size, flags); + } + + public bool Commit(ulong offset, ulong size) => _impl.Commit(offset, size); + public void MapAsRx(ulong offset, ulong size) => _impl.Reprotect(offset, size, MemoryPermission.ReadAndExecute); + public void MapAsRwx(ulong offset, ulong size) => _impl.Reprotect(offset, size, MemoryPermission.ReadWriteExecute); + + public void Dispose() => _impl.Dispose(); + } +} diff --git a/ARMeilleure/Memory/MemoryHelper.cs b/Ryujinx.Cpu/MemoryHelper.cs similarity index 80% rename from ARMeilleure/Memory/MemoryHelper.cs rename to Ryujinx.Cpu/MemoryHelper.cs index 8e310d4d..5ba5ef38 100644 --- a/ARMeilleure/Memory/MemoryHelper.cs +++ b/Ryujinx.Cpu/MemoryHelper.cs @@ -1,9 +1,9 @@ -using System; +using System; using System.IO; using System.Runtime.InteropServices; using System.Text; -namespace ARMeilleure.Memory +namespace Ryujinx.Cpu { public static class MemoryHelper { @@ -13,12 +13,12 @@ namespace ARMeilleure.Memory for (int offs = 0; offs < size8; offs += 8) { - memory.WriteInt64(position + offs, 0); + memory.Write((ulong)(position + offs), 0); } for (int offs = size8; offs < (size - size8); offs++) { - memory.WriteByte(position + offs, 0); + memory.Write((ulong)(position + offs), 0); } } @@ -26,7 +26,9 @@ namespace ARMeilleure.Memory { long size = Marshal.SizeOf(); - byte[] data = memory.ReadBytes(position, size); + byte[] data = new byte[size]; + + memory.Read((ulong)position, data); fixed (byte* ptr = data) { @@ -45,7 +47,7 @@ namespace ARMeilleure.Memory Marshal.StructureToPtr(value, (IntPtr)ptr, false); } - memory.WriteBytes(position, data); + memory.Write((ulong)position, data); } public static string ReadAsciiString(MemoryManager memory, long position, long maxSize = -1) @@ -54,7 +56,7 @@ namespace ARMeilleure.Memory { for (long offs = 0; offs < maxSize || maxSize == -1; offs++) { - byte value = (byte)memory.ReadByte(position + offs); + byte value = memory.Read((ulong)(position + offs)); if (value == 0) { diff --git a/Ryujinx.Cpu/MemoryManager.cs b/Ryujinx.Cpu/MemoryManager.cs new file mode 100644 index 00000000..001c41f8 --- /dev/null +++ b/Ryujinx.Cpu/MemoryManager.cs @@ -0,0 +1,466 @@ +using ARMeilleure.Memory; +using Ryujinx.Memory; +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Threading; + +namespace Ryujinx.Cpu +{ + /// + /// Represents a CPU memory manager. + /// + public sealed class MemoryManager : IMemoryManager, IDisposable + { + public const int PageBits = 12; + public const int PageSize = 1 << PageBits; + public const int PageMask = PageSize - 1; + + private const int PteSize = 8; + + public int AddressSpaceBits { get; } + + private readonly ulong _addressSpaceSize; + + private readonly MemoryBlock _backingMemory; + private readonly MemoryBlock _pageTable; + + public IntPtr PageTablePointer => _pageTable.Pointer; + + /// + /// Creates a new instance of the memory manager. + /// + /// Physical backing memory where virtual memory will be mapped to + /// Size of the address space + public MemoryManager(MemoryBlock backingMemory, ulong addressSpaceSize) + { + ulong asSize = PageSize; + int asBits = PageBits; + + while (asSize < addressSpaceSize) + { + asSize <<= 1; + asBits++; + } + + AddressSpaceBits = asBits; + _addressSpaceSize = asSize; + _backingMemory = backingMemory; + _pageTable = new MemoryBlock((asSize / PageSize) * PteSize); + } + + /// + /// Maps a virtual memory range into a physical memory range. + /// + /// + /// Addresses and size must be page aligned. + /// + /// Virtual memory address + /// Physical memory address + /// Size to be mapped + public void Map(ulong va, ulong pa, ulong size) + { + while (size != 0) + { + _pageTable.Write((va / PageSize) * PteSize, PaToPte(pa)); + + va += PageSize; + pa += PageSize; + size -= PageSize; + } + } + + /// + /// Unmaps a previously mapped range of virtual memory. + /// + /// Virtual address of the range to be unmapped + /// Size of the range to be unmapped + public void Unmap(ulong va, ulong size) + { + while (size != 0) + { + _pageTable.Write((va / PageSize) * PteSize, 0UL); + + va += PageSize; + size -= PageSize; + } + } + + /// + /// Reads data from CPU mapped memory. + /// + /// Type of the data being read + /// Virtual address of the data in memory + /// The data + public T Read(ulong va) where T : unmanaged + { + return MemoryMarshal.Cast(GetSpan(va, Unsafe.SizeOf()))[0]; + } + + /// + /// Reads data from CPU mapped memory. + /// + /// Virtual address of the data in memory + /// Span to store the data being read into + public void Read(ulong va, Span data) + { + ReadImpl(va, data); + } + + /// + /// Writes data to CPU mapped memory. + /// + /// Type of the data being written + /// Virtual address to write the data into + /// Data to be written + public void Write(ulong va, T value) where T : unmanaged + { + Write(va, MemoryMarshal.Cast(MemoryMarshal.CreateSpan(ref value, 1))); + } + + /// + /// Writes data to CPU mapped memory. + /// + /// Virtual address to write the data into + /// Data to be written + public void Write(ulong va, ReadOnlySpan data) + { + if (data.Length == 0) + { + return; + } + + MarkRegionAsModified(va, (ulong)data.Length); + + if (IsContiguous(va, data.Length)) + { + data.CopyTo(_backingMemory.GetSpan(GetPhysicalAddressInternal(va), data.Length)); + } + else + { + int offset = 0, size; + + if ((va & PageMask) != 0) + { + ulong pa = GetPhysicalAddressInternal(va); + + size = Math.Min(data.Length, PageSize - (int)(va & PageMask)); + + data.Slice(0, size).CopyTo(_backingMemory.GetSpan(pa, size)); + + offset += size; + } + + for (; offset < data.Length; offset += size) + { + ulong pa = GetPhysicalAddressInternal(va + (ulong)offset); + + size = Math.Min(data.Length - offset, PageSize); + + data.Slice(offset, size).CopyTo(_backingMemory.GetSpan(pa, size)); + } + } + } + + /// + /// Gets a read-only span of data from CPU mapped memory. + /// + /// + /// This may perform a allocation if the data is not contiguous in memory. + /// For this reason, the span is read-only, you can't modify the data. + /// + /// Virtual address of the data + /// Size of the data + /// A read-only span of the data + public ReadOnlySpan GetSpan(ulong va, int size) + { + if (size == 0) + { + return ReadOnlySpan.Empty; + } + + if (IsContiguous(va, size)) + { + return _backingMemory.GetSpan(GetPhysicalAddressInternal(va), size); + } + else + { + Span data = new byte[size]; + + ReadImpl(va, data); + + return data; + } + } + + /// + /// Gets a reference for the given type at the specified virtual memory address. + /// + /// + /// The data must be located at a contiguous memory region. + /// + /// Type of the data to get the reference + /// Virtual address of the data + /// A reference to the data in memory + public ref T GetRef(ulong va) where T : unmanaged + { + if (!IsContiguous(va, Unsafe.SizeOf())) + { + ThrowMemoryNotContiguous(); + } + + MarkRegionAsModified(va, (ulong)Unsafe.SizeOf()); + + return ref _backingMemory.GetRef(GetPhysicalAddressInternal(va)); + } + + private void ThrowMemoryNotContiguous() => throw new MemoryNotContiguousException(); + + // TODO: Remove that once we have proper 8-bits and 16-bits CAS. + public ref T GetRefNoChecks(ulong va) where T : unmanaged + { + MarkRegionAsModified(va, (ulong)Unsafe.SizeOf()); + + return ref _backingMemory.GetRef(GetPhysicalAddressInternal(va)); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private bool IsContiguous(ulong va, int size) + { + if (!ValidateAddress(va)) + { + return false; + } + + ulong endVa = (va + (ulong)size + PageMask) & ~(ulong)PageMask; + + va &= ~(ulong)PageMask; + + int pages = (int)((endVa - va) / PageSize); + + for (int page = 0; page < pages - 1; page++) + { + if (!ValidateAddress(va + PageSize)) + { + return false; + } + + if (GetPhysicalAddressInternal(va) + PageSize != GetPhysicalAddressInternal(va + PageSize)) + { + return false; + } + + va += PageSize; + } + + return true; + } + + private void ReadImpl(ulong va, Span data) + { + if (data.Length == 0) + { + return; + } + + 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.Slice(0, 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)); + } + } + + /// + /// Checks if a specified virtual memory region has been modified by the CPU since the last call. + /// + /// Virtual address of the region + /// Size of the region + /// Resource identifier number (maximum is 15) + /// Optional array where the modified ranges should be written + /// The number of modified ranges + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int QueryModified(ulong va, ulong size, int id, (ulong, ulong)[] modifiedRanges = null) + { + if (!ValidateAddress(va)) + { + return 0; + } + + ulong maxSize = _addressSpaceSize - va; + + if (size > maxSize) + { + size = maxSize; + } + + // We need to ensure that the tagged pointer value is negative, + // JIT generated code checks that to take the slow paths and call the MemoryManager Read/Write methods. + long tag = (0x8000L | (1L << id)) << 48; + + ulong endVa = (va + size + PageMask) & ~(ulong)PageMask; + + va &= ~(ulong)PageMask; + + ulong rgStart = va; + ulong rgSize = 0; + + int rangeIndex = 0; + + for (; va < endVa; va += PageSize) + { + while (true) + { + ref long pte = ref _pageTable.GetRef((va >> PageBits) * PteSize); + + long pteValue = pte; + + // If the PTE value is 0, that means that the page is unmapped. + // We behave as if the page was not modified, since modifying a page + // that is not even mapped is impossible. + if ((pteValue & tag) == tag || pteValue == 0) + { + if (rgSize != 0) + { + if (modifiedRanges != null && rangeIndex < modifiedRanges.Length) + { + modifiedRanges[rangeIndex] = (rgStart, rgSize); + } + + rangeIndex++; + + rgSize = 0; + } + + break; + } + else + { + if (Interlocked.CompareExchange(ref pte, pteValue | tag, pteValue) == pteValue) + { + if (rgSize == 0) + { + rgStart = va; + } + + rgSize += PageSize; + + break; + } + } + } + } + + if (rgSize != 0) + { + if (modifiedRanges != null && rangeIndex < modifiedRanges.Length) + { + modifiedRanges[rangeIndex] = (rgStart, rgSize); + } + + rangeIndex++; + } + + return rangeIndex; + } + + /// + /// Checks if the page at a given CPU virtual address. + /// + /// Virtual address to check + /// True if the address is mapped, false otherwise + public bool IsMapped(ulong va) + { + if (!ValidateAddress(va)) + { + return false; + } + + return _pageTable.Read((va / PageSize) * PteSize) != 0; + } + + private bool ValidateAddress(ulong va) + { + return va < _addressSpaceSize; + } + + /// + /// Performs address translation of the address inside a CPU mapped memory range. + /// + /// + /// If the address is invalid or unmapped, -1 will be returned. + /// + /// Virtual address to be translated + /// The physical address + public ulong GetPhysicalAddress(ulong va) + { + // We return -1L if the virtual address is invalid or unmapped. + if (!ValidateAddress(va) || !IsMapped(va)) + { + return ulong.MaxValue; + } + + return GetPhysicalAddressInternal(va); + } + + private ulong GetPhysicalAddressInternal(ulong va) + { + return PteToPa(_pageTable.Read((va / PageSize) * PteSize) & ~(0xffffUL << 48)) + (va & PageMask); + } + + private void MarkRegionAsModified(ulong va, ulong size) + { + ulong endVa = (va + size + PageMask) & ~(ulong)PageMask; + + while (va < endVa) + { + ref long pageRef = ref _pageTable.GetRef((va >> PageBits) * PteSize); + + long pte; + + do + { + pte = Volatile.Read(ref pageRef); + + if (pte >= 0) + { + break; + } + } + while (Interlocked.CompareExchange(ref pageRef, pte & ~(0xffffL << 48), pte) != pte); + + va += PageSize; + } + } + + private ulong PaToPte(ulong pa) + { + return (ulong)_backingMemory.GetPointer(pa, PageSize).ToInt64(); + } + + private ulong PteToPa(ulong pte) + { + return (ulong)((long)pte - _backingMemory.Pointer.ToInt64()); + } + + public void Dispose() + { + _pageTable.Dispose(); + } + } +} diff --git a/Ryujinx.Cpu/MemoryNotContiguousException.cs b/Ryujinx.Cpu/MemoryNotContiguousException.cs new file mode 100644 index 00000000..d7c827cc --- /dev/null +++ b/Ryujinx.Cpu/MemoryNotContiguousException.cs @@ -0,0 +1,19 @@ +using System; + +namespace Ryujinx.Cpu +{ + class MemoryNotContiguousException : Exception + { + public MemoryNotContiguousException() : base("The specified memory region is not contiguous.") + { + } + + public MemoryNotContiguousException(string message) : base(message) + { + } + + public MemoryNotContiguousException(string message, Exception innerException) : base(message, innerException) + { + } + } +} \ No newline at end of file diff --git a/Ryujinx.Cpu/Ryujinx.Cpu.csproj b/Ryujinx.Cpu/Ryujinx.Cpu.csproj new file mode 100644 index 00000000..3fd647eb --- /dev/null +++ b/Ryujinx.Cpu/Ryujinx.Cpu.csproj @@ -0,0 +1,20 @@ + + + + netcoreapp3.1 + + + + true + + + + true + + + + + + + + diff --git a/Ryujinx.Graphics.Gpu/GpuContext.cs b/Ryujinx.Graphics.Gpu/GpuContext.cs index f2a0d5e5..5e117831 100644 --- a/Ryujinx.Graphics.Gpu/GpuContext.cs +++ b/Ryujinx.Graphics.Gpu/GpuContext.cs @@ -108,7 +108,7 @@ namespace Ryujinx.Graphics.Gpu /// This is required for any GPU memory access. /// /// CPU memory manager - public void SetVmm(ARMeilleure.Memory.MemoryManager cpuMemory) + public void SetVmm(Cpu.MemoryManager cpuMemory) { PhysicalMemory = new PhysicalMemory(cpuMemory); } diff --git a/Ryujinx.Graphics.Gpu/Image/Pool.cs b/Ryujinx.Graphics.Gpu/Image/Pool.cs index 7cf06d0b..8796894c 100644 --- a/Ryujinx.Graphics.Gpu/Image/Pool.cs +++ b/Ryujinx.Graphics.Gpu/Image/Pool.cs @@ -33,6 +33,8 @@ namespace Ryujinx.Graphics.Gpu.Image /// public ulong Size { get; } + private readonly (ulong, ulong)[] _modifiedRanges; + public Pool(GpuContext context, ulong address, int maximumId) { Context = context; @@ -46,6 +48,8 @@ namespace Ryujinx.Graphics.Gpu.Image Address = address; Size = size; + + _modifiedRanges = new (ulong, ulong)[size / PhysicalMemory.PageSize]; } /// @@ -62,11 +66,11 @@ namespace Ryujinx.Graphics.Gpu.Image /// public void SynchronizeMemory() { - (ulong, ulong)[] modifiedRanges = Context.PhysicalMemory.GetModifiedRanges(Address, Size, ResourceName.TexturePool); + int count = Context.PhysicalMemory.QueryModified(Address, Size, ResourceName.TexturePool, _modifiedRanges); - for (int index = 0; index < modifiedRanges.Length; index++) + for (int index = 0; index < count; index++) { - (ulong mAddress, ulong mSize) = modifiedRanges[index]; + (ulong mAddress, ulong mSize) = _modifiedRanges[index]; if (mAddress < Address) { @@ -84,6 +88,11 @@ namespace Ryujinx.Graphics.Gpu.Image } } + private void InvalidateRangeInternal(ulong offset, int size) + { + InvalidateRangeImpl(Address + offset, (ulong)size); + } + /// /// Invalidates a range of memory of the GPU resource pool. /// Entries that falls inside the speicified range will be invalidated, diff --git a/Ryujinx.Graphics.Gpu/Image/Texture.cs b/Ryujinx.Graphics.Gpu/Image/Texture.cs index d02c3665..9554bc6b 100644 --- a/Ryujinx.Graphics.Gpu/Image/Texture.cs +++ b/Ryujinx.Graphics.Gpu/Image/Texture.cs @@ -78,6 +78,8 @@ namespace Ryujinx.Graphics.Gpu.Image /// public ulong Size => (ulong)_sizeInfo.TotalSize; + private (ulong, ulong)[] _modifiedRanges; + private int _referenceCount; private int _sequenceNumber; @@ -133,6 +135,8 @@ namespace Ryujinx.Graphics.Gpu.Image _context = context; _sizeInfo = sizeInfo; + _modifiedRanges = new (ulong, ulong)[(sizeInfo.TotalSize / PhysicalMemory.PageSize) + 1]; + SetInfo(info); _viewStorage = this; @@ -304,9 +308,9 @@ namespace Ryujinx.Graphics.Gpu.Image _sequenceNumber = _context.SequenceNumber; - (ulong, ulong)[] modifiedRanges = _context.PhysicalMemory.GetModifiedRanges(Address, Size, ResourceName.Texture); + int modifiedCount = _context.PhysicalMemory.QueryModified(Address, Size, ResourceName.Texture, _modifiedRanges); - if (modifiedRanges.Length == 0 && _hasData) + if (modifiedCount == 0 && _hasData) { return; } @@ -325,9 +329,9 @@ namespace Ryujinx.Graphics.Gpu.Image ulong endAddress = Address + Size; - for (int i = 0; i < modifiedRanges.Length; i++) + for (int i = 0; i < modifiedCount; i++) { - (ulong modifiedAddress, ulong modifiedSize) = modifiedRanges[i]; + (ulong modifiedAddress, ulong modifiedSize) = _modifiedRanges[i]; ulong endModifiedAddress = modifiedAddress + modifiedSize; diff --git a/Ryujinx.Graphics.Gpu/Memory/Buffer.cs b/Ryujinx.Graphics.Gpu/Memory/Buffer.cs index e4bc161f..959e1a10 100644 --- a/Ryujinx.Graphics.Gpu/Memory/Buffer.cs +++ b/Ryujinx.Graphics.Gpu/Memory/Buffer.cs @@ -30,7 +30,9 @@ namespace Ryujinx.Graphics.Gpu.Memory /// public ulong EndAddress => Address + Size; - private int[] _sequenceNumbers; + private readonly (ulong, ulong)[] _modifiedRanges; + + private readonly int[] _sequenceNumbers; /// /// Creates a new instance of the buffer. @@ -46,6 +48,8 @@ namespace Ryujinx.Graphics.Gpu.Memory HostBuffer = context.Renderer.CreateBuffer((int)size); + _modifiedRanges = new (ulong, ulong)[size / PhysicalMemory.PageSize]; + _sequenceNumbers = new int[size / MemoryManager.PageSize]; } @@ -113,11 +117,11 @@ namespace Ryujinx.Graphics.Gpu.Memory return; } - (ulong, ulong)[] modifiedRanges = _context.PhysicalMemory.GetModifiedRanges(address, size, ResourceName.Buffer); + int count = _context.PhysicalMemory.QueryModified(address, size, ResourceName.Buffer, _modifiedRanges); - for (int index = 0; index < modifiedRanges.Length; index++) + for (int index = 0; index < count; index++) { - (ulong mAddress, ulong mSize) = modifiedRanges[index]; + (ulong mAddress, ulong mSize) = _modifiedRanges[index]; int offset = (int)(mAddress - Address); diff --git a/Ryujinx.Graphics.Gpu/Memory/PhysicalMemory.cs b/Ryujinx.Graphics.Gpu/Memory/PhysicalMemory.cs index ca28f31d..a787305d 100644 --- a/Ryujinx.Graphics.Gpu/Memory/PhysicalMemory.cs +++ b/Ryujinx.Graphics.Gpu/Memory/PhysicalMemory.cs @@ -1,22 +1,23 @@ using System; +using System.Runtime.CompilerServices; namespace Ryujinx.Graphics.Gpu.Memory { - using CpuMemoryManager = ARMeilleure.Memory.MemoryManager; - /// /// Represents physical memory, accessible from the GPU. /// This is actually working CPU virtual addresses, of memory mapped on the application process. /// class PhysicalMemory { - private readonly CpuMemoryManager _cpuMemory; + public const int PageSize = Cpu.MemoryManager.PageSize; + + private readonly Cpu.MemoryManager _cpuMemory; /// /// Creates a new instance of the physical memory. /// /// CPU memory manager of the application process - public PhysicalMemory(CpuMemoryManager cpuMemory) + public PhysicalMemory(Cpu.MemoryManager cpuMemory) { _cpuMemory = cpuMemory; } @@ -29,7 +30,7 @@ namespace Ryujinx.Graphics.Gpu.Memory /// A read only span of the data at the specified memory location public ReadOnlySpan GetSpan(ulong address, ulong size) { - return _cpuMemory.GetSpan(address, size); + return _cpuMemory.GetSpan(address, (int)size); } /// @@ -39,19 +40,21 @@ namespace Ryujinx.Graphics.Gpu.Memory /// Data to be written public void Write(ulong address, ReadOnlySpan data) { - _cpuMemory.WriteBytes((long)address, data.ToArray()); + _cpuMemory.Write(address, data); } /// - /// Gets the modified ranges for a given range of the application process mapped memory. + /// Checks if a specified virtual memory region has been modified by the CPU since the last call. /// - /// Start address of the range - /// Size, in bytes, of the range - /// Name of the GPU resource being checked - /// Ranges, composed of address and size, modified by the application process, form the CPU - public (ulong, ulong)[] GetModifiedRanges(ulong address, ulong size, ResourceName name) + /// CPU virtual address of the region + /// Size of the region + /// Resource name + /// Optional array where the modified ranges should be written + /// The number of modified ranges + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int QueryModified(ulong address, ulong size, ResourceName name, (ulong, ulong)[] modifiedRanges = null) { - return _cpuMemory.GetModifiedRanges(address, size, (int)name); + return _cpuMemory.QueryModified(address, size, (int)name, modifiedRanges); } } } \ No newline at end of file diff --git a/Ryujinx.Graphics.Gpu/Ryujinx.Graphics.Gpu.csproj b/Ryujinx.Graphics.Gpu/Ryujinx.Graphics.Gpu.csproj index 49e93aa1..d846b64c 100644 --- a/Ryujinx.Graphics.Gpu/Ryujinx.Graphics.Gpu.csproj +++ b/Ryujinx.Graphics.Gpu/Ryujinx.Graphics.Gpu.csproj @@ -1,7 +1,7 @@ - + diff --git a/Ryujinx.HLE/DeviceMemory.cs b/Ryujinx.HLE/DeviceMemory.cs deleted file mode 100644 index 22945b83..00000000 --- a/Ryujinx.HLE/DeviceMemory.cs +++ /dev/null @@ -1,200 +0,0 @@ -using ARMeilleure.Memory; -using System; -using System.Runtime.InteropServices; -using System.Runtime.CompilerServices; - -namespace Ryujinx.HLE -{ - class DeviceMemory : IDisposable - { - public const long RamSize = 4L * 1024 * 1024 * 1024; - - public IntPtr RamPointer { get; } - - private unsafe byte* _ramPtr; - - public unsafe DeviceMemory() - { - RamPointer = MemoryManagement.AllocateWriteTracked(RamSize); - - _ramPtr = (byte*)RamPointer; - } - - public sbyte ReadSByte(long position) - { - return (sbyte)ReadByte(position); - } - - public short ReadInt16(long position) - { - return (short)ReadUInt16(position); - } - - public int ReadInt32(long position) - { - return (int)ReadUInt32(position); - } - - public long ReadInt64(long position) - { - return (long)ReadUInt64(position); - } - - public unsafe byte ReadByte(long position) - { - return *(_ramPtr + position); - } - - public unsafe ushort ReadUInt16(long position) - { - return *((ushort*)(_ramPtr + position)); - } - - public unsafe uint ReadUInt32(long position) - { - return *((uint*)(_ramPtr + position)); - } - - public unsafe ulong ReadUInt64(long position) - { - return *((ulong*)(_ramPtr + position)); - } - - public unsafe T ReadStruct(long position) - { - return Marshal.PtrToStructure((IntPtr)(_ramPtr + position)); - } - - public unsafe ref T GetStructRef(long position) - { - return ref Unsafe.AsRef((void*)(IntPtr)(_ramPtr + position)); - } - - public void WriteSByte(long position, sbyte value) - { - WriteByte(position, (byte)value); - } - - public void WriteInt16(long position, short value) - { - WriteUInt16(position, (ushort)value); - } - - public void WriteInt32(long position, int value) - { - WriteUInt32(position, (uint)value); - } - - public void WriteInt64(long position, long value) - { - WriteUInt64(position, (ulong)value); - } - - public unsafe void WriteByte(long position, byte value) - { - *(_ramPtr + position) = value; - } - - public unsafe void WriteUInt16(long position, ushort value) - { - *((ushort*)(_ramPtr + position)) = value; - } - - public unsafe void WriteUInt32(long position, uint value) - { - *((uint*)(_ramPtr + position)) = value; - } - - public unsafe void WriteUInt64(long position, ulong value) - { - *((ulong*)(_ramPtr + position)) = value; - } - - public unsafe void WriteStruct(long position, T value) - { - Marshal.StructureToPtr(value, (IntPtr)(_ramPtr + position), false); - } - - public void FillWithZeros(long position, int size) - { - int size8 = size & ~(8 - 1); - - for (int offs = 0; offs < size8; offs += 8) - { - WriteInt64(position + offs, 0); - } - - for (int offs = size8; offs < (size - size8); offs++) - { - WriteByte(position + offs, 0); - } - } - - public void Set(ulong address, byte value, ulong size) - { - if (address + size < address) - { - throw new ArgumentOutOfRangeException(nameof(size)); - } - - if (address + size > RamSize) - { - throw new ArgumentOutOfRangeException(nameof(address)); - } - - ulong size8 = size & ~7UL; - - ulong valueRep = (ulong)value * 0x0101010101010101; - - for (ulong offs = 0; offs < size8; offs += 8) - { - WriteUInt64((long)(address + offs), valueRep); - } - - for (ulong offs = size8; offs < (size - size8); offs++) - { - WriteByte((long)(address + offs), value); - } - } - - public void Copy(ulong dst, ulong src, ulong size) - { - if (dst + size < dst || src + size < src) - { - throw new ArgumentOutOfRangeException(nameof(size)); - } - - if (dst + size > RamSize) - { - throw new ArgumentOutOfRangeException(nameof(dst)); - } - - if (src + size > RamSize) - { - throw new ArgumentOutOfRangeException(nameof(src)); - } - - ulong size8 = size & ~7UL; - - for (ulong offs = 0; offs < size8; offs += 8) - { - WriteUInt64((long)(dst + offs), ReadUInt64((long)(src + offs))); - } - - for (ulong offs = size8; offs < (size - size8); offs++) - { - WriteByte((long)(dst + offs), ReadByte((long)(src + offs))); - } - } - - public void Dispose() - { - Dispose(true); - } - - protected virtual void Dispose(bool disposing) - { - MemoryManagement.Free(RamPointer); - } - } -} diff --git a/Ryujinx.HLE/HOS/Font/SharedFontManager.cs b/Ryujinx.HLE/HOS/Font/SharedFontManager.cs index 5201e0f3..34129404 100644 --- a/Ryujinx.HLE/HOS/Font/SharedFontManager.cs +++ b/Ryujinx.HLE/HOS/Font/SharedFontManager.cs @@ -19,7 +19,7 @@ namespace Ryujinx.HLE.HOS.Font { private Switch _device; - private long _physicalAddress; + private ulong _physicalAddress; private struct FontInfo { @@ -35,7 +35,7 @@ namespace Ryujinx.HLE.HOS.Font private Dictionary _fontData; - public SharedFontManager(Switch device, long physicalAddress) + public SharedFontManager(Switch device, ulong physicalAddress) { _physicalAddress = physicalAddress; _device = device; @@ -52,7 +52,7 @@ namespace Ryujinx.HLE.HOS.Font { if (_fontData == null) { - _device.Memory.FillWithZeros(_physicalAddress, Horizon.FontSize); + _device.Memory.ZeroFill(_physicalAddress, Horizon.FontSize); uint fontOffset = 0; @@ -67,7 +67,7 @@ namespace Ryujinx.HLE.HOS.Font if (!string.IsNullOrWhiteSpace(fontPath)) { byte[] data; - + using (IStorage ncaFileStream = new LocalStorage(fontPath, FileAccess.Read, FileMode.Open)) { Nca nca = new Nca(_device.System.KeySet, ncaFileStream); @@ -77,7 +77,7 @@ namespace Ryujinx.HLE.HOS.Font data = DecryptFont(fontFile.AsStream()); } - + FontInfo info = new FontInfo((int)fontOffset, data.Length); WriteMagicAndSize(_physicalAddress + fontOffset, data.Length); @@ -88,7 +88,7 @@ namespace Ryujinx.HLE.HOS.Font for (; fontOffset - start < data.Length; fontOffset++) { - _device.Memory.WriteByte(_physicalAddress + fontOffset, data[fontOffset - start]); + _device.Memory.Write(_physicalAddress + fontOffset, data[fontOffset - start]); } return info; @@ -129,15 +129,15 @@ namespace Ryujinx.HLE.HOS.Font } } - private void WriteMagicAndSize(long position, int size) + private void WriteMagicAndSize(ulong address, int size) { const int decMagic = 0x18029a7f; const int key = 0x49621806; int encryptedSize = BinaryPrimitives.ReverseEndianness(size ^ key); - _device.Memory.WriteInt32(position + 0, decMagic); - _device.Memory.WriteInt32(position + 4, encryptedSize); + _device.Memory.Write(address + 0, decMagic); + _device.Memory.Write(address + 4, encryptedSize); } public int GetFontSize(SharedFontType fontType) diff --git a/Ryujinx.HLE/HOS/Homebrew.cs b/Ryujinx.HLE/HOS/Homebrew.cs deleted file mode 100644 index dca02d91..00000000 --- a/Ryujinx.HLE/HOS/Homebrew.cs +++ /dev/null @@ -1,77 +0,0 @@ -using ARMeilleure.Memory; -using System.Text; - -namespace Ryujinx.HLE.HOS -{ - static class Homebrew - { - public const string TemporaryNroSuffix = ".ryu_tmp.nro"; - - // http://switchbrew.org/index.php?title=Homebrew_ABI - public static void WriteHbAbiData(MemoryManager memory, long position, int mainThreadHandle, string switchPath) - { - // MainThreadHandle. - WriteConfigEntry(memory, ref position, 1, 0, mainThreadHandle); - - // NextLoadPath. - WriteConfigEntry(memory, ref position, 2, 0, position + 0x200, position + 0x400); - - // Argv. - long argvPosition = position + 0xC00; - - memory.WriteBytes(argvPosition, Encoding.ASCII.GetBytes(switchPath + "\0")); - - WriteConfigEntry(memory, ref position, 5, 0, 0, argvPosition); - - // AppletType. - WriteConfigEntry(memory, ref position, 7); - - // EndOfList. - WriteConfigEntry(memory, ref position, 0); - } - - private static void WriteConfigEntry( - MemoryManager memory, - ref long position, - int key, - int flags = 0, - long value0 = 0, - long value1 = 0) - { - memory.WriteInt32(position + 0x00, key); - memory.WriteInt32(position + 0x04, flags); - memory.WriteInt64(position + 0x08, value0); - memory.WriteInt64(position + 0x10, value1); - - position += 0x18; - } - - public static string ReadHbAbiNextLoadPath(MemoryManager memory, long position) - { - string fileName = null; - - while (true) - { - long key = memory.ReadInt64(position); - - if (key == 2) - { - long value0 = memory.ReadInt64(position + 0x08); - long value1 = memory.ReadInt64(position + 0x10); - - fileName = MemoryHelper.ReadAsciiString(memory, value0, value1 - value0); - - break; - } - else if (key == 0) - { - break; - } - - position += 0x18; - } - - return fileName; - } - } -} diff --git a/Ryujinx.HLE/HOS/Horizon.cs b/Ryujinx.HLE/HOS/Horizon.cs index 76185d7b..37c3fba7 100644 --- a/Ryujinx.HLE/HOS/Horizon.cs +++ b/Ryujinx.HLE/HOS/Horizon.cs @@ -141,7 +141,7 @@ namespace Ryujinx.HLE.HOS public int GlobalAccessLogMode { get; set; } - internal long HidBaseAddress { get; private set; } + internal ulong HidBaseAddress { get; private set; } internal NvHostSyncpt HostSyncpoint { get; private set; } @@ -202,7 +202,7 @@ namespace Ryujinx.HLE.HOS ulong iirsPa = region.Address + HidSize + FontSize; ulong timePa = region.Address + HidSize + FontSize + IirsSize; - HidBaseAddress = (long)(hidPa - DramMemoryMap.DramBase); + HidBaseAddress = hidPa - DramMemoryMap.DramBase; KPageList hidPageList = new KPageList(); KPageList fontPageList = new KPageList(); @@ -220,13 +220,13 @@ namespace Ryujinx.HLE.HOS KSharedMemory timeSharedMemory = new KSharedMemory(this, timePageList, 0, 0, MemoryPermission.Read); - TimeServiceManager.Instance.Initialize(device, this, timeSharedMemory, (long)(timePa - DramMemoryMap.DramBase), TimeSize); + TimeServiceManager.Instance.Initialize(device, this, timeSharedMemory, timePa - DramMemoryMap.DramBase, TimeSize); AppletState = new AppletStateMgr(this); AppletState.SetFocus(true); - Font = new SharedFontManager(device, (long)(fontPa - DramMemoryMap.DramBase)); + Font = new SharedFontManager(device, fontPa - DramMemoryMap.DramBase); IUserInterface.InitializePort(this); diff --git a/Ryujinx.HLE/HOS/Ipc/IpcHandler.cs b/Ryujinx.HLE/HOS/Ipc/IpcHandler.cs index 4b201d14..43eb4a4e 100644 --- a/Ryujinx.HLE/HOS/Ipc/IpcHandler.cs +++ b/Ryujinx.HLE/HOS/Ipc/IpcHandler.cs @@ -1,4 +1,4 @@ -using ARMeilleure.Memory; +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Ipc; using Ryujinx.HLE.HOS.Kernel.Process; @@ -103,7 +103,7 @@ namespace Ryujinx.HLE.HOS.Ipc throw new NotImplementedException(request.Type.ToString()); } - memory.WriteBytes(cmdPtr, response.GetBytes(cmdPtr)); + memory.Write((ulong)cmdPtr, response.GetBytes(cmdPtr)); } return KernelResult.Success; diff --git a/Ryujinx.HLE/HOS/Kernel/Common/KernelTransfer.cs b/Ryujinx.HLE/HOS/Kernel/Common/KernelTransfer.cs index 62330d6b..cd8d6661 100644 --- a/Ryujinx.HLE/HOS/Kernel/Common/KernelTransfer.cs +++ b/Ryujinx.HLE/HOS/Kernel/Common/KernelTransfer.cs @@ -1,5 +1,5 @@ +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Kernel.Process; -using ARMeilleure.Memory; namespace Ryujinx.HLE.HOS.Kernel.Common { @@ -9,10 +9,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Common { KProcess currentProcess = system.Scheduler.GetCurrentProcess(); - if (currentProcess.CpuMemory.IsMapped((long)address) && - currentProcess.CpuMemory.IsMapped((long)address + 3)) + if (currentProcess.CpuMemory.IsMapped(address) && + currentProcess.CpuMemory.IsMapped(address + 3)) { - value = currentProcess.CpuMemory.ReadInt32((long)address); + value = currentProcess.CpuMemory.Read(address); return true; } @@ -28,10 +28,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Common for (int index = 0; index < values.Length; index++, address += 4) { - if (currentProcess.CpuMemory.IsMapped((long)address) && - currentProcess.CpuMemory.IsMapped((long)address + 3)) + if (currentProcess.CpuMemory.IsMapped(address) && + currentProcess.CpuMemory.IsMapped(address + 3)) { - values[index]= currentProcess.CpuMemory.ReadInt32((long)address); + values[index]= currentProcess.CpuMemory.Read(address); } else { @@ -46,8 +46,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Common { KProcess currentProcess = system.Scheduler.GetCurrentProcess(); - if (currentProcess.CpuMemory.IsMapped((long)address) && - currentProcess.CpuMemory.IsMapped((long)address + size - 1)) + if (currentProcess.CpuMemory.IsMapped(address) && + currentProcess.CpuMemory.IsMapped(address + (ulong)size - 1)) { value = MemoryHelper.ReadAsciiString(currentProcess.CpuMemory, (long)address, size); @@ -63,10 +63,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Common { KProcess currentProcess = system.Scheduler.GetCurrentProcess(); - if (currentProcess.CpuMemory.IsMapped((long)address) && - currentProcess.CpuMemory.IsMapped((long)address + 3)) + if (currentProcess.CpuMemory.IsMapped(address) && + currentProcess.CpuMemory.IsMapped(address + 3)) { - currentProcess.CpuMemory.WriteInt32((long)address, value); + currentProcess.CpuMemory.Write(address, value); return true; } @@ -78,10 +78,10 @@ namespace Ryujinx.HLE.HOS.Kernel.Common { KProcess currentProcess = system.Scheduler.GetCurrentProcess(); - if (currentProcess.CpuMemory.IsMapped((long)address) && - currentProcess.CpuMemory.IsMapped((long)address + 7)) + if (currentProcess.CpuMemory.IsMapped(address) && + currentProcess.CpuMemory.IsMapped(address + 7)) { - currentProcess.CpuMemory.WriteInt64((long)address, value); + currentProcess.CpuMemory.Write(address, value); return true; } diff --git a/Ryujinx.HLE/HOS/Kernel/Ipc/KServerSession.cs b/Ryujinx.HLE/HOS/Kernel/Ipc/KServerSession.cs index 7fba645f..201b0bec 100644 --- a/Ryujinx.HLE/HOS/Kernel/Ipc/KServerSession.cs +++ b/Ryujinx.HLE/HOS/Kernel/Ipc/KServerSession.cs @@ -322,8 +322,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc serverHeader.ReceiveListType, serverHeader.ReceiveListOffset); - serverProcess.CpuMemory.WriteUInt32((long)serverMsg.Address + 0, clientHeader.Word0); - serverProcess.CpuMemory.WriteUInt32((long)serverMsg.Address + 4, clientHeader.Word1); + serverProcess.CpuMemory.Write(serverMsg.Address + 0, clientHeader.Word0); + serverProcess.CpuMemory.Write(serverMsg.Address + 4, clientHeader.Word1); uint offset; @@ -337,13 +337,13 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc return KernelResult.InvalidCombination; } - serverProcess.CpuMemory.WriteUInt32((long)serverMsg.Address + 8, clientHeader.Word2); + serverProcess.CpuMemory.Write(serverMsg.Address + 8, clientHeader.Word2); offset = 3; if (clientHeader.HasPid) { - serverProcess.CpuMemory.WriteInt64((long)serverMsg.Address + offset * 4, clientProcess.Pid); + serverProcess.CpuMemory.Write(serverMsg.Address + offset * 4, clientProcess.Pid); offset += 2; } @@ -352,14 +352,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc { int newHandle = 0; - int handle = System.Device.Memory.ReadInt32((long)clientMsg.DramAddress + offset * 4); + int handle = System.Device.Memory.Read(clientMsg.DramAddress + offset * 4); if (clientResult == KernelResult.Success && handle != 0) { clientResult = GetCopyObjectHandle(clientThread, serverProcess, handle, out newHandle); } - serverProcess.CpuMemory.WriteInt32((long)serverMsg.Address + offset * 4, newHandle); + serverProcess.CpuMemory.Write(serverMsg.Address + offset * 4, newHandle); offset++; } @@ -368,7 +368,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc { int newHandle = 0; - int handle = System.Device.Memory.ReadInt32((long)clientMsg.DramAddress + offset * 4); + int handle = System.Device.Memory.Read(clientMsg.DramAddress + offset * 4); if (handle != 0) { @@ -382,7 +382,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc } } - serverProcess.CpuMemory.WriteInt32((long)serverMsg.Address + offset * 4, newHandle); + serverProcess.CpuMemory.Write(serverMsg.Address + offset * 4, newHandle); offset++; } @@ -404,7 +404,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc for (int index = 0; index < clientHeader.PointerBuffersCount; index++) { - ulong pointerDesc = System.Device.Memory.ReadUInt64((long)clientMsg.DramAddress + offset * 4); + ulong pointerDesc = System.Device.Memory.Read(clientMsg.DramAddress + offset * 4); PointerBufferDesc descriptor = new PointerBufferDesc(pointerDesc); @@ -450,7 +450,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc descriptor.BufferAddress = 0; } - serverProcess.CpuMemory.WriteUInt64((long)serverMsg.Address + offset * 4, descriptor.Pack()); + serverProcess.CpuMemory.Write(serverMsg.Address + offset * 4, descriptor.Pack()); offset += 2; } @@ -463,11 +463,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc for (int index = 0; index < totalBuffersCount; index++) { - long clientDescAddress = (long)clientMsg.DramAddress + offset * 4; + ulong clientDescAddress = clientMsg.DramAddress + offset * 4; - uint descWord0 = System.Device.Memory.ReadUInt32(clientDescAddress + 0); - uint descWord1 = System.Device.Memory.ReadUInt32(clientDescAddress + 4); - uint descWord2 = System.Device.Memory.ReadUInt32(clientDescAddress + 8); + uint descWord0 = System.Device.Memory.Read(clientDescAddress + 0); + uint descWord1 = System.Device.Memory.Read(clientDescAddress + 4); + uint descWord2 = System.Device.Memory.Read(clientDescAddress + 8); bool isSendDesc = index < clientHeader.SendBuffersCount; bool isExchangeDesc = index >= clientHeader.SendBuffersCount + clientHeader.ReceiveBuffersCount; @@ -542,11 +542,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc descWord2 |= (uint)(dstAddress >> 34) & 0x3ffffffc; descWord2 |= (uint)(dstAddress >> 4) & 0xf0000000; - long serverDescAddress = (long)serverMsg.Address + offset * 4; + ulong serverDescAddress = serverMsg.Address + offset * 4; - serverProcess.CpuMemory.WriteUInt32(serverDescAddress + 0, descWord0); - serverProcess.CpuMemory.WriteUInt32(serverDescAddress + 4, descWord1); - serverProcess.CpuMemory.WriteUInt32(serverDescAddress + 8, descWord2); + serverProcess.CpuMemory.Write(serverDescAddress + 0, descWord0); + serverProcess.CpuMemory.Write(serverDescAddress + 4, descWord1); + serverProcess.CpuMemory.Write(serverDescAddress + 8, descWord2); offset += 3; } @@ -700,8 +700,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc } // Copy header. - System.Device.Memory.WriteUInt32((long)clientMsg.DramAddress + 0, serverHeader.Word0); - System.Device.Memory.WriteUInt32((long)clientMsg.DramAddress + 4, serverHeader.Word1); + System.Device.Memory.Write(clientMsg.DramAddress + 0, serverHeader.Word0); + System.Device.Memory.Write(clientMsg.DramAddress + 4, serverHeader.Word1); // Copy handles. uint offset; @@ -710,11 +710,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc { offset = 3; - System.Device.Memory.WriteUInt32((long)clientMsg.DramAddress + 8, serverHeader.Word2); + System.Device.Memory.Write(clientMsg.DramAddress + 8, serverHeader.Word2); if (serverHeader.HasPid) { - System.Device.Memory.WriteInt64((long)clientMsg.DramAddress + offset * 4, serverProcess.Pid); + System.Device.Memory.Write(clientMsg.DramAddress + offset * 4, serverProcess.Pid); offset += 2; } @@ -723,14 +723,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc { int newHandle = 0; - int handle = serverProcess.CpuMemory.ReadInt32((long)serverMsg.Address + offset * 4); + int handle = serverProcess.CpuMemory.Read(serverMsg.Address + offset * 4); if (handle != 0) { GetCopyObjectHandle(serverThread, clientProcess, handle, out newHandle); } - System.Device.Memory.WriteInt32((long)clientMsg.DramAddress + offset * 4, newHandle); + System.Device.Memory.Write(clientMsg.DramAddress + offset * 4, newHandle); offset++; } @@ -739,7 +739,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc { int newHandle = 0; - int handle = serverProcess.CpuMemory.ReadInt32((long)serverMsg.Address + offset * 4); + int handle = serverProcess.CpuMemory.Read(serverMsg.Address + offset * 4); if (handle != 0) { @@ -753,7 +753,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc } } - System.Device.Memory.WriteInt32((long)clientMsg.DramAddress + offset * 4, newHandle); + System.Device.Memory.Write(clientMsg.DramAddress + offset * 4, newHandle); offset++; } @@ -768,7 +768,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc for (int index = 0; index < serverHeader.PointerBuffersCount; index++) { - ulong pointerDesc = serverProcess.CpuMemory.ReadUInt64((long)serverMsg.Address + offset * 4); + ulong pointerDesc = serverProcess.CpuMemory.Read(serverMsg.Address + offset * 4); PointerBufferDesc descriptor = new PointerBufferDesc(pointerDesc); @@ -819,11 +819,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc for (int index = 0; index < totalBuffersCount; index++) { - long dstDescAddress = (long)clientMsg.DramAddress + offset * 4; + ulong dstDescAddress = clientMsg.DramAddress + offset * 4; - System.Device.Memory.WriteUInt32(dstDescAddress + 0, 0); - System.Device.Memory.WriteUInt32(dstDescAddress + 4, 0); - System.Device.Memory.WriteUInt32(dstDescAddress + 8, 0); + System.Device.Memory.Write(dstDescAddress + 0, 0); + System.Device.Memory.Write(dstDescAddress + 4, 0); + System.Device.Memory.Write(dstDescAddress + 8, 0); offset += 3; } @@ -878,9 +878,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc private MessageHeader GetClientMessageHeader(Message clientMsg) { - uint word0 = System.Device.Memory.ReadUInt32((long)clientMsg.DramAddress + 0); - uint word1 = System.Device.Memory.ReadUInt32((long)clientMsg.DramAddress + 4); - uint word2 = System.Device.Memory.ReadUInt32((long)clientMsg.DramAddress + 8); + uint word0 = System.Device.Memory.Read(clientMsg.DramAddress + 0); + uint word1 = System.Device.Memory.Read(clientMsg.DramAddress + 4); + uint word2 = System.Device.Memory.Read(clientMsg.DramAddress + 8); return new MessageHeader(word0, word1, word2); } @@ -889,9 +889,9 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc { KProcess currentProcess = System.Scheduler.GetCurrentProcess(); - uint word0 = currentProcess.CpuMemory.ReadUInt32((long)serverMsg.Address + 0); - uint word1 = currentProcess.CpuMemory.ReadUInt32((long)serverMsg.Address + 4); - uint word2 = currentProcess.CpuMemory.ReadUInt32((long)serverMsg.Address + 8); + uint word0 = currentProcess.CpuMemory.Read(serverMsg.Address + 0); + uint word1 = currentProcess.CpuMemory.Read(serverMsg.Address + 4); + uint word2 = currentProcess.CpuMemory.Read(serverMsg.Address + 8); return new MessageHeader(word0, word1, word2); } @@ -970,11 +970,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc ulong[] receiveList = new ulong[recvListSize]; - long recvListAddress = (long)message.DramAddress + recvListOffset; + ulong recvListAddress = message.DramAddress + recvListOffset; for (int index = 0; index < recvListSize; index++) { - receiveList[index] = System.Device.Memory.ReadUInt64(recvListAddress + index * 8); + receiveList[index] = System.Device.Memory.Read(recvListAddress + (ulong)index * 8); } return receiveList; @@ -1067,20 +1067,20 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc if (header.HasPid) { - process.CpuMemory.WriteInt64((long)message.Address + offset * 4, 0); + process.CpuMemory.Write(message.Address + offset * 4, 0L); offset += 2; } for (int index = 0; index < totalHandeslCount; index++) { - int handle = process.CpuMemory.ReadInt32((long)message.Address + offset * 4); + int handle = process.CpuMemory.Read(message.Address + offset * 4); if (handle != 0) { process.HandleTable.CloseHandle(handle); - process.CpuMemory.WriteInt32((long)message.Address + offset * 4, 0); + process.CpuMemory.Write(message.Address + offset * 4, 0); } offset++; @@ -1225,8 +1225,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Ipc ulong address = clientProcess.MemoryManager.GetDramAddressFromVa(request.CustomCmdBuffAddr); - System.Device.Memory.WriteInt64((long)address + 0, 0); - System.Device.Memory.WriteInt32((long)address + 8, (int)result); + System.Device.Memory.Write(address, 0); + System.Device.Memory.Write(address + 8, (int)result); clientProcess.MemoryManager.UnborrowIpcBuffer( request.CustomCmdBuffAddr, diff --git a/Ryujinx.HLE/HOS/Kernel/Memory/KMemoryManager.cs b/Ryujinx.HLE/HOS/Kernel/Memory/KMemoryManager.cs index 3379e912..9dcacd30 100644 --- a/Ryujinx.HLE/HOS/Kernel/Memory/KMemoryManager.cs +++ b/Ryujinx.HLE/HOS/Kernel/Memory/KMemoryManager.cs @@ -1,5 +1,5 @@ -using ARMeilleure.Memory; using Ryujinx.Common; +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Process; using System; @@ -1843,7 +1843,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory { ulong unusedSizeBefore = address - addressTruncated; - _system.Device.Memory.Set(dstFirstPagePa, 0, unusedSizeBefore); + _system.Device.Memory.ZeroFill(dstFirstPagePa, unusedSizeBefore); ulong copySize = addressRounded <= endAddr ? addressRounded - address : size; @@ -1862,7 +1862,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory if (unusedSizeAfter != 0) { - _system.Device.Memory.Set(firstPageFillAddress, 0, unusedSizeAfter); + _system.Device.Memory.ZeroFill(firstPageFillAddress, unusedSizeAfter); } KPageList pages = new KPageList(); @@ -1922,7 +1922,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory unusedSizeAfter = PageSize; } - _system.Device.Memory.Set(lastPageFillAddr, 0, unusedSizeAfter); + _system.Device.Memory.ZeroFill(lastPageFillAddr, unusedSizeAfter); if (pages.AddRange(dstFirstPagePa, 1) != KernelResult.Success) { @@ -3041,7 +3041,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory { ulong size = pagesCount * PageSize; - _cpuMemory.Map((long)dstVa, (long)(srcPa - DramMemoryMap.DramBase), (long)size); + _cpuMemory.Map(dstVa, srcPa - DramMemoryMap.DramBase, size); result = KernelResult.Success; @@ -3066,7 +3066,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory { ulong size = pagesCount * PageSize; - _cpuMemory.Unmap((long)dstVa, (long)size); + _cpuMemory.Unmap(dstVa, size); result = KernelResult.Success; @@ -3108,7 +3108,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory { ulong size = pageNode.PagesCount * PageSize; - _cpuMemory.Map((long)address, (long)(pageNode.Address - DramMemoryMap.DramBase), (long)size); + _cpuMemory.Map(address, pageNode.Address - DramMemoryMap.DramBase, size); address += size; } @@ -3118,12 +3118,12 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory public ulong GetDramAddressFromVa(ulong va) { - return (ulong)_cpuMemory.GetPhysicalAddress((long)va); + return _cpuMemory.GetPhysicalAddress(va); } public bool ConvertVaToPa(ulong va, out ulong pa) { - pa = DramMemoryMap.DramBase + (ulong)_cpuMemory.GetPhysicalAddress((long)va); + pa = DramMemoryMap.DramBase + _cpuMemory.GetPhysicalAddress(va); return true; } diff --git a/Ryujinx.HLE/HOS/Kernel/Process/HleProcessDebugger.cs b/Ryujinx.HLE/HOS/Kernel/Process/HleProcessDebugger.cs index 75f52851..376badfb 100644 --- a/Ryujinx.HLE/HOS/Kernel/Process/HleProcessDebugger.cs +++ b/Ryujinx.HLE/HOS/Kernel/Process/HleProcessDebugger.cs @@ -1,12 +1,9 @@ -using ARMeilleure.Memory; -using Ryujinx.Common; +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Diagnostics.Demangler; using Ryujinx.HLE.HOS.Kernel.Memory; using Ryujinx.HLE.Loaders.Elf; using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Runtime.CompilerServices; using System.Text; using System.Threading; @@ -20,11 +17,11 @@ namespace Ryujinx.HLE.HOS.Kernel.Process private class Image { - public long BaseAddress { get; private set; } + public ulong BaseAddress { get; } - public ElfSymbol[] Symbols { get; private set; } + public ElfSymbol[] Symbols { get; } - public Image(long baseAddress, ElfSymbol[] symbols) + public Image(ulong baseAddress, ElfSymbol[] symbols) { BaseAddress = baseAddress; Symbols = symbols; @@ -48,7 +45,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process StringBuilder trace = new StringBuilder(); - void AppendTrace(long address) + void AppendTrace(ulong address) { Image image = GetImage(address, out int imageIndex); @@ -63,7 +60,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process if (image != null) { - long offset = address - image.BaseAddress; + ulong offset = address - image.BaseAddress; string imageName = GetGuessedNsoNameFromIndex(imageIndex); @@ -79,7 +76,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process if (context.IsAarch32) { - long framePointer = (long)context.GetX(11); + ulong framePointer = context.GetX(11); while (framePointer != 0) { @@ -90,14 +87,14 @@ namespace Ryujinx.HLE.HOS.Kernel.Process break; } - AppendTrace(_owner.CpuMemory.ReadInt32(framePointer + 4)); + AppendTrace(_owner.CpuMemory.Read(framePointer + 4)); - framePointer = _owner.CpuMemory.ReadInt32(framePointer); + framePointer = _owner.CpuMemory.Read(framePointer); } } else { - long framePointer = (long)context.GetX(29); + ulong framePointer = context.GetX(29); while (framePointer != 0) { @@ -108,16 +105,16 @@ namespace Ryujinx.HLE.HOS.Kernel.Process break; } - AppendTrace(_owner.CpuMemory.ReadInt64(framePointer + 8)); + AppendTrace(_owner.CpuMemory.Read(framePointer + 8)); - framePointer = _owner.CpuMemory.ReadInt64(framePointer); + framePointer = _owner.CpuMemory.Read(framePointer); } } return trace.ToString(); } - private bool TryGetSubName(Image image, long address, out string name) + private bool TryGetSubName(Image image, ulong address, out string name) { address -= image.BaseAddress; @@ -156,13 +153,13 @@ namespace Ryujinx.HLE.HOS.Kernel.Process return false; } - private Image GetImage(long address, out int index) + private Image GetImage(ulong address, out int index) { lock (_images) { for (index = _images.Count - 1; index >= 0; index--) { - if ((ulong)address >= (ulong)_images[index].BaseAddress) + if (address >= _images[index].BaseAddress) { return _images[index]; } @@ -229,7 +226,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process if (info.State == MemoryState.CodeStatic && info.Permission == MemoryPermission.ReadAndExecute) { - LoadMod0Symbols(_owner.CpuMemory, (long)info.Address); + LoadMod0Symbols(_owner.CpuMemory, info.Address); } oldAddress = address; @@ -238,54 +235,53 @@ namespace Ryujinx.HLE.HOS.Kernel.Process } } - private void LoadMod0Symbols(MemoryManager memory, long textOffset) + private void LoadMod0Symbols(MemoryManager memory, ulong textOffset) { - long mod0Offset = textOffset + memory.ReadUInt32(textOffset + 4); + ulong mod0Offset = textOffset + memory.Read(textOffset + 4); if (mod0Offset < textOffset || !memory.IsMapped(mod0Offset) || (mod0Offset & 3) != 0) { return; } - Dictionary dynamic = new Dictionary(); + Dictionary dynamic = new Dictionary(); - int mod0Magic = memory.ReadInt32(mod0Offset + 0x0); + int mod0Magic = memory.Read(mod0Offset + 0x0); if (mod0Magic != Mod0) { return; } - long dynamicOffset = memory.ReadInt32(mod0Offset + 0x4) + mod0Offset; - long bssStartOffset = memory.ReadInt32(mod0Offset + 0x8) + mod0Offset; - long bssEndOffset = memory.ReadInt32(mod0Offset + 0xc) + mod0Offset; - long ehHdrStartOffset = memory.ReadInt32(mod0Offset + 0x10) + mod0Offset; - long ehHdrEndOffset = memory.ReadInt32(mod0Offset + 0x14) + mod0Offset; - long modObjOffset = memory.ReadInt32(mod0Offset + 0x18) + mod0Offset; + ulong dynamicOffset = memory.Read(mod0Offset + 0x4) + mod0Offset; + ulong bssStartOffset = memory.Read(mod0Offset + 0x8) + mod0Offset; + ulong bssEndOffset = memory.Read(mod0Offset + 0xc) + mod0Offset; + ulong ehHdrStartOffset = memory.Read(mod0Offset + 0x10) + mod0Offset; + ulong ehHdrEndOffset = memory.Read(mod0Offset + 0x14) + mod0Offset; + ulong modObjOffset = memory.Read(mod0Offset + 0x18) + mod0Offset; - bool isAArch32 = memory.ReadUInt64(dynamicOffset) > 0xFFFFFFFF || memory.ReadUInt64(dynamicOffset + 0x10) > 0xFFFFFFFF; + bool isAArch32 = memory.Read(dynamicOffset) > 0xFFFFFFFF || memory.Read(dynamicOffset + 0x10) > 0xFFFFFFFF; while (true) { - long tagVal; - long value; + ulong tagVal; + ulong value; if (isAArch32) { - tagVal = memory.ReadInt32(dynamicOffset + 0); - value = memory.ReadInt32(dynamicOffset + 4); + tagVal = memory.Read(dynamicOffset + 0); + value = memory.Read(dynamicOffset + 4); dynamicOffset += 0x8; } else { - tagVal = memory.ReadInt64(dynamicOffset + 0); - value = memory.ReadInt64(dynamicOffset + 8); + tagVal = memory.Read(dynamicOffset + 0); + value = memory.Read(dynamicOffset + 8); dynamicOffset += 0x10; } - ElfDynamicTag tag = (ElfDynamicTag)tagVal; if (tag == ElfDynamicTag.DT_NULL) @@ -296,19 +292,19 @@ namespace Ryujinx.HLE.HOS.Kernel.Process dynamic[tag] = value; } - if (!dynamic.TryGetValue(ElfDynamicTag.DT_STRTAB, out long strTab) || - !dynamic.TryGetValue(ElfDynamicTag.DT_SYMTAB, out long symTab) || - !dynamic.TryGetValue(ElfDynamicTag.DT_SYMENT, out long symEntSize)) + if (!dynamic.TryGetValue(ElfDynamicTag.DT_STRTAB, out ulong strTab) || + !dynamic.TryGetValue(ElfDynamicTag.DT_SYMTAB, out ulong symTab) || + !dynamic.TryGetValue(ElfDynamicTag.DT_SYMENT, out ulong symEntSize)) { return; } - long strTblAddr = textOffset + strTab; - long symTblAddr = textOffset + symTab; + ulong strTblAddr = textOffset + strTab; + ulong symTblAddr = textOffset + symTab; List symbols = new List(); - while ((ulong)symTblAddr < (ulong)strTblAddr) + while (symTblAddr < strTblAddr) { ElfSymbol sym = isAArch32 ? GetSymbol32(memory, symTblAddr, strTblAddr) : GetSymbol64(memory, symTblAddr, strTblAddr); @@ -323,42 +319,36 @@ namespace Ryujinx.HLE.HOS.Kernel.Process } } - private ElfSymbol GetSymbol64(MemoryManager memory, long address, long strTblAddr) + private ElfSymbol GetSymbol64(MemoryManager memory, ulong address, ulong strTblAddr) { - using (BinaryReader inputStream = new BinaryReader(new MemoryStream(memory.ReadBytes(address, Unsafe.SizeOf())))) + ElfSymbol64 sym = memory.Read(address); + + uint nameIndex = sym.NameOffset; + + string name = string.Empty; + + for (int chr; (chr = memory.Read(strTblAddr + nameIndex++)) != 0;) { - ElfSymbol64 sym = inputStream.ReadStruct(); - - uint nameIndex = sym.NameOffset; - - string name = string.Empty; - - for (int chr; (chr = memory.ReadByte(strTblAddr + nameIndex++)) != 0;) - { - name += (char)chr; - } - - return new ElfSymbol(name, sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size); + name += (char)chr; } + + return new ElfSymbol(name, sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size); } - private ElfSymbol GetSymbol32(MemoryManager memory, long address, long strTblAddr) + private ElfSymbol GetSymbol32(MemoryManager memory, ulong address, ulong strTblAddr) { - using (BinaryReader inputStream = new BinaryReader(new MemoryStream(memory.ReadBytes(address, Unsafe.SizeOf())))) + ElfSymbol32 sym = memory.Read(address); + + uint nameIndex = sym.NameOffset; + + string name = string.Empty; + + for (int chr; (chr = memory.Read(strTblAddr + nameIndex++)) != 0;) { - ElfSymbol32 sym = inputStream.ReadStruct(); - - uint nameIndex = sym.NameOffset; - - string name = string.Empty; - - for (int chr; (chr = memory.ReadByte(strTblAddr + nameIndex++)) != 0;) - { - name += (char)chr; - } - - return new ElfSymbol(name, sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size); + name += (char)chr; } + + return new ElfSymbol(name, sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size); } } } \ No newline at end of file diff --git a/Ryujinx.HLE/HOS/Kernel/Process/KProcess.cs b/Ryujinx.HLE/HOS/Kernel/Process/KProcess.cs index 7807ec5a..c7d11130 100644 --- a/Ryujinx.HLE/HOS/Kernel/Process/KProcess.cs +++ b/Ryujinx.HLE/HOS/Kernel/Process/KProcess.cs @@ -1,7 +1,6 @@ -using ARMeilleure.Memory; using ARMeilleure.State; -using ARMeilleure.Translation; using Ryujinx.Common; +using Ryujinx.Cpu; using Ryujinx.HLE.Exceptions; using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Memory; @@ -79,8 +78,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Process public bool IsPaused { get; private set; } public MemoryManager CpuMemory { get; private set; } - - public Translator Translator { get; private set; } + public CpuContext CpuContext { get; private set; } private SvcHandler _svcHandler; @@ -1109,11 +1107,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Process default: throw new ArgumentException(nameof(addrSpaceType)); } - bool useFlatPageTable = memRegion == MemoryRegion.Application; - - CpuMemory = new MemoryManager(_system.Device.Memory.RamPointer, addrSpaceBits, useFlatPageTable); - - Translator = new Translator(CpuMemory); + CpuMemory = new MemoryManager(_system.Device.Memory, 1UL << addrSpaceBits); + CpuContext = new CpuContext(CpuMemory); // TODO: This should eventually be removed. // The GPU shouldn't depend on the CPU memory manager at all. diff --git a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcIpc.cs b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcIpc.cs index bcecf86a..7188ead0 100644 --- a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcIpc.cs +++ b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcIpc.cs @@ -108,7 +108,9 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall private KernelResult SendSyncRequest(ulong messagePtr, ulong size, int handle) { - byte[] messageData = _process.CpuMemory.ReadBytes((long)messagePtr, (long)size); + byte[] messageData = new byte[size]; + + _process.CpuMemory.Read(messagePtr, messageData); KClientSession clientSession = _process.HandleTable.GetObject(handle); diff --git a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcMemory.cs b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcMemory.cs index b69ce03a..a6d06986 100644 --- a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcMemory.cs +++ b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcMemory.cs @@ -191,14 +191,14 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall { KMemoryInfo blkInfo = _process.MemoryManager.QueryMemory(position); - _process.CpuMemory.WriteUInt64((long)infoPtr + 0x00, blkInfo.Address); - _process.CpuMemory.WriteUInt64((long)infoPtr + 0x08, blkInfo.Size); - _process.CpuMemory.WriteInt32 ((long)infoPtr + 0x10, (int)blkInfo.State & 0xff); - _process.CpuMemory.WriteInt32 ((long)infoPtr + 0x14, (int)blkInfo.Attribute); - _process.CpuMemory.WriteInt32 ((long)infoPtr + 0x18, (int)blkInfo.Permission); - _process.CpuMemory.WriteInt32 ((long)infoPtr + 0x1c, blkInfo.IpcRefCount); - _process.CpuMemory.WriteInt32 ((long)infoPtr + 0x20, blkInfo.DeviceRefCount); - _process.CpuMemory.WriteInt32 ((long)infoPtr + 0x24, 0); + _process.CpuMemory.Write(infoPtr + 0x00, blkInfo.Address); + _process.CpuMemory.Write(infoPtr + 0x08, blkInfo.Size); + _process.CpuMemory.Write(infoPtr + 0x10, (int)blkInfo.State & 0xff); + _process.CpuMemory.Write(infoPtr + 0x14, (int)blkInfo.Attribute); + _process.CpuMemory.Write(infoPtr + 0x18, (int)blkInfo.Permission); + _process.CpuMemory.Write(infoPtr + 0x1c, blkInfo.IpcRefCount); + _process.CpuMemory.Write(infoPtr + 0x20, blkInfo.DeviceRefCount); + _process.CpuMemory.Write(infoPtr + 0x24, 0); pageInfo = 0; diff --git a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcSystem.cs b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcSystem.cs index 82c30cb5..f654630c 100644 --- a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcSystem.cs +++ b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcSystem.cs @@ -1,6 +1,6 @@ -using ARMeilleure.Memory; using Ryujinx.Common; using Ryujinx.Common.Logging; +using Ryujinx.Cpu; using Ryujinx.HLE.Exceptions; using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Ipc; diff --git a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcThread.cs b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcThread.cs index 101fb192..f1ac3ded 100644 --- a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcThread.cs +++ b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcThread.cs @@ -1,5 +1,5 @@ -using ARMeilleure.Memory; using ARMeilleure.State; +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Process; using Ryujinx.HLE.HOS.Kernel.Threading; @@ -432,79 +432,79 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall MemoryManager memory = currentProcess.CpuMemory; - memory.WriteUInt64((long)address + 0x0, thread.Context.GetX(0)); - memory.WriteUInt64((long)address + 0x8, thread.Context.GetX(1)); - memory.WriteUInt64((long)address + 0x10, thread.Context.GetX(2)); - memory.WriteUInt64((long)address + 0x18, thread.Context.GetX(3)); - memory.WriteUInt64((long)address + 0x20, thread.Context.GetX(4)); - memory.WriteUInt64((long)address + 0x28, thread.Context.GetX(5)); - memory.WriteUInt64((long)address + 0x30, thread.Context.GetX(6)); - memory.WriteUInt64((long)address + 0x38, thread.Context.GetX(7)); - memory.WriteUInt64((long)address + 0x40, thread.Context.GetX(8)); - memory.WriteUInt64((long)address + 0x48, thread.Context.GetX(9)); - memory.WriteUInt64((long)address + 0x50, thread.Context.GetX(10)); - memory.WriteUInt64((long)address + 0x58, thread.Context.GetX(11)); - memory.WriteUInt64((long)address + 0x60, thread.Context.GetX(12)); - memory.WriteUInt64((long)address + 0x68, thread.Context.GetX(13)); - memory.WriteUInt64((long)address + 0x70, thread.Context.GetX(14)); - memory.WriteUInt64((long)address + 0x78, thread.Context.GetX(15)); - memory.WriteUInt64((long)address + 0x80, thread.Context.GetX(16)); - memory.WriteUInt64((long)address + 0x88, thread.Context.GetX(17)); - memory.WriteUInt64((long)address + 0x90, thread.Context.GetX(18)); - memory.WriteUInt64((long)address + 0x98, thread.Context.GetX(19)); - memory.WriteUInt64((long)address + 0xa0, thread.Context.GetX(20)); - memory.WriteUInt64((long)address + 0xa8, thread.Context.GetX(21)); - memory.WriteUInt64((long)address + 0xb0, thread.Context.GetX(22)); - memory.WriteUInt64((long)address + 0xb8, thread.Context.GetX(23)); - memory.WriteUInt64((long)address + 0xc0, thread.Context.GetX(24)); - memory.WriteUInt64((long)address + 0xc8, thread.Context.GetX(25)); - memory.WriteUInt64((long)address + 0xd0, thread.Context.GetX(26)); - memory.WriteUInt64((long)address + 0xd8, thread.Context.GetX(27)); - memory.WriteUInt64((long)address + 0xe0, thread.Context.GetX(28)); - memory.WriteUInt64((long)address + 0xe8, thread.Context.GetX(29)); - memory.WriteUInt64((long)address + 0xf0, thread.Context.GetX(30)); - memory.WriteUInt64((long)address + 0xf8, thread.Context.GetX(31)); + memory.Write(address + 0x0, thread.Context.GetX(0)); + memory.Write(address + 0x8, thread.Context.GetX(1)); + memory.Write(address + 0x10, thread.Context.GetX(2)); + memory.Write(address + 0x18, thread.Context.GetX(3)); + memory.Write(address + 0x20, thread.Context.GetX(4)); + memory.Write(address + 0x28, thread.Context.GetX(5)); + memory.Write(address + 0x30, thread.Context.GetX(6)); + memory.Write(address + 0x38, thread.Context.GetX(7)); + memory.Write(address + 0x40, thread.Context.GetX(8)); + memory.Write(address + 0x48, thread.Context.GetX(9)); + memory.Write(address + 0x50, thread.Context.GetX(10)); + memory.Write(address + 0x58, thread.Context.GetX(11)); + memory.Write(address + 0x60, thread.Context.GetX(12)); + memory.Write(address + 0x68, thread.Context.GetX(13)); + memory.Write(address + 0x70, thread.Context.GetX(14)); + memory.Write(address + 0x78, thread.Context.GetX(15)); + memory.Write(address + 0x80, thread.Context.GetX(16)); + memory.Write(address + 0x88, thread.Context.GetX(17)); + memory.Write(address + 0x90, thread.Context.GetX(18)); + memory.Write(address + 0x98, thread.Context.GetX(19)); + memory.Write(address + 0xa0, thread.Context.GetX(20)); + memory.Write(address + 0xa8, thread.Context.GetX(21)); + memory.Write(address + 0xb0, thread.Context.GetX(22)); + memory.Write(address + 0xb8, thread.Context.GetX(23)); + memory.Write(address + 0xc0, thread.Context.GetX(24)); + memory.Write(address + 0xc8, thread.Context.GetX(25)); + memory.Write(address + 0xd0, thread.Context.GetX(26)); + memory.Write(address + 0xd8, thread.Context.GetX(27)); + memory.Write(address + 0xe0, thread.Context.GetX(28)); + memory.Write(address + 0xe8, thread.Context.GetX(29)); + memory.Write(address + 0xf0, thread.Context.GetX(30)); + memory.Write(address + 0xf8, thread.Context.GetX(31)); - memory.WriteInt64((long)address + 0x100, thread.LastPc); + memory.Write(address + 0x100, thread.LastPc); - memory.WriteUInt64((long)address + 0x108, (ulong)GetPsr(thread.Context)); + memory.Write(address + 0x108, (ulong)GetPsr(thread.Context)); - memory.WriteVector128((long)address + 0x110, thread.Context.GetV(0)); - memory.WriteVector128((long)address + 0x120, thread.Context.GetV(1)); - memory.WriteVector128((long)address + 0x130, thread.Context.GetV(2)); - memory.WriteVector128((long)address + 0x140, thread.Context.GetV(3)); - memory.WriteVector128((long)address + 0x150, thread.Context.GetV(4)); - memory.WriteVector128((long)address + 0x160, thread.Context.GetV(5)); - memory.WriteVector128((long)address + 0x170, thread.Context.GetV(6)); - memory.WriteVector128((long)address + 0x180, thread.Context.GetV(7)); - memory.WriteVector128((long)address + 0x190, thread.Context.GetV(8)); - memory.WriteVector128((long)address + 0x1a0, thread.Context.GetV(9)); - memory.WriteVector128((long)address + 0x1b0, thread.Context.GetV(10)); - memory.WriteVector128((long)address + 0x1c0, thread.Context.GetV(11)); - memory.WriteVector128((long)address + 0x1d0, thread.Context.GetV(12)); - memory.WriteVector128((long)address + 0x1e0, thread.Context.GetV(13)); - memory.WriteVector128((long)address + 0x1f0, thread.Context.GetV(14)); - memory.WriteVector128((long)address + 0x200, thread.Context.GetV(15)); - memory.WriteVector128((long)address + 0x210, thread.Context.GetV(16)); - memory.WriteVector128((long)address + 0x220, thread.Context.GetV(17)); - memory.WriteVector128((long)address + 0x230, thread.Context.GetV(18)); - memory.WriteVector128((long)address + 0x240, thread.Context.GetV(19)); - memory.WriteVector128((long)address + 0x250, thread.Context.GetV(20)); - memory.WriteVector128((long)address + 0x260, thread.Context.GetV(21)); - memory.WriteVector128((long)address + 0x270, thread.Context.GetV(22)); - memory.WriteVector128((long)address + 0x280, thread.Context.GetV(23)); - memory.WriteVector128((long)address + 0x290, thread.Context.GetV(24)); - memory.WriteVector128((long)address + 0x2a0, thread.Context.GetV(25)); - memory.WriteVector128((long)address + 0x2b0, thread.Context.GetV(26)); - memory.WriteVector128((long)address + 0x2c0, thread.Context.GetV(27)); - memory.WriteVector128((long)address + 0x2d0, thread.Context.GetV(28)); - memory.WriteVector128((long)address + 0x2e0, thread.Context.GetV(29)); - memory.WriteVector128((long)address + 0x2f0, thread.Context.GetV(30)); - memory.WriteVector128((long)address + 0x300, thread.Context.GetV(31)); + memory.Write(address + 0x110, thread.Context.GetV(0)); + memory.Write(address + 0x120, thread.Context.GetV(1)); + memory.Write(address + 0x130, thread.Context.GetV(2)); + memory.Write(address + 0x140, thread.Context.GetV(3)); + memory.Write(address + 0x150, thread.Context.GetV(4)); + memory.Write(address + 0x160, thread.Context.GetV(5)); + memory.Write(address + 0x170, thread.Context.GetV(6)); + memory.Write(address + 0x180, thread.Context.GetV(7)); + memory.Write(address + 0x190, thread.Context.GetV(8)); + memory.Write(address + 0x1a0, thread.Context.GetV(9)); + memory.Write(address + 0x1b0, thread.Context.GetV(10)); + memory.Write(address + 0x1c0, thread.Context.GetV(11)); + memory.Write(address + 0x1d0, thread.Context.GetV(12)); + memory.Write(address + 0x1e0, thread.Context.GetV(13)); + memory.Write(address + 0x1f0, thread.Context.GetV(14)); + memory.Write(address + 0x200, thread.Context.GetV(15)); + memory.Write(address + 0x210, thread.Context.GetV(16)); + memory.Write(address + 0x220, thread.Context.GetV(17)); + memory.Write(address + 0x230, thread.Context.GetV(18)); + memory.Write(address + 0x240, thread.Context.GetV(19)); + memory.Write(address + 0x250, thread.Context.GetV(20)); + memory.Write(address + 0x260, thread.Context.GetV(21)); + memory.Write(address + 0x270, thread.Context.GetV(22)); + memory.Write(address + 0x280, thread.Context.GetV(23)); + memory.Write(address + 0x290, thread.Context.GetV(24)); + memory.Write(address + 0x2a0, thread.Context.GetV(25)); + memory.Write(address + 0x2b0, thread.Context.GetV(26)); + memory.Write(address + 0x2c0, thread.Context.GetV(27)); + memory.Write(address + 0x2d0, thread.Context.GetV(28)); + memory.Write(address + 0x2e0, thread.Context.GetV(29)); + memory.Write(address + 0x2f0, thread.Context.GetV(30)); + memory.Write(address + 0x300, thread.Context.GetV(31)); - memory.WriteInt32((long)address + 0x310, (int)thread.Context.Fpcr); - memory.WriteInt32((long)address + 0x314, (int)thread.Context.Fpsr); - memory.WriteInt64((long)address + 0x318, thread.Context.Tpidr); + memory.Write(address + 0x310, (int)thread.Context.Fpcr); + memory.Write(address + 0x314, (int)thread.Context.Fpsr); + memory.Write(address + 0x318, thread.Context.Tpidr); return KernelResult.Success; } diff --git a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcThreadSync.cs b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcThreadSync.cs index 4adbdd62..eecafb0b 100644 --- a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcThreadSync.cs +++ b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SvcThreadSync.cs @@ -37,7 +37,7 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall for (int index = 0; index < handlesCount; index++) { - int handle = _process.CpuMemory.ReadInt32((long)handlesPtr + index * 4); + int handle = _process.CpuMemory.Read(handlesPtr + (ulong)index * 4); KSynchronizationObject syncObj = _process.HandleTable.GetObject(handle); diff --git a/Ryujinx.HLE/HOS/Kernel/Threading/KAddressArbiter.cs b/Ryujinx.HLE/HOS/Kernel/Threading/KAddressArbiter.cs index 166cf064..71ad712e 100644 --- a/Ryujinx.HLE/HOS/Kernel/Threading/KAddressArbiter.cs +++ b/Ryujinx.HLE/HOS/Kernel/Threading/KAddressArbiter.cs @@ -2,6 +2,7 @@ using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Process; using System.Collections.Generic; using System.Linq; +using System.Threading; namespace Ryujinx.HLE.HOS.Kernel.Threading { @@ -228,18 +229,22 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading KProcess currentProcess = _system.Scheduler.GetCurrentProcess(); + if (!currentProcess.CpuMemory.IsMapped(address)) + { + // Invalid address. + requester.SignaledObj = null; + requester.ObjSyncResult = KernelResult.InvalidMemState; + + return null; + } + + ref int mutexRef = ref currentProcess.CpuMemory.GetRef(address); + int mutexValue, newMutexValue; do { - if (!KernelTransfer.UserToKernelInt32(_system, address, out mutexValue)) - { - // Invalid address. - requester.SignaledObj = null; - requester.ObjSyncResult = KernelResult.InvalidMemState; - - return null; - } + mutexValue = mutexRef; if (mutexValue != 0) { @@ -252,7 +257,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading newMutexValue = requester.ThreadHandleForUserMutex; } } - while (!currentProcess.CpuMemory.AtomicCompareExchangeInt32((long)address, mutexValue, newMutexValue)); + while (Interlocked.CompareExchange(ref mutexRef, newMutexValue, mutexValue) != mutexValue); if (mutexValue == 0) { @@ -389,7 +394,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading if (shouldDecrement) { - currentValue = currentProcess.CpuMemory.AtomicDecrementInt32((long)address) + 1; + currentValue = Interlocked.Decrement(ref currentProcess.CpuMemory.GetRef(address)) + 1; } if (currentValue < value) @@ -480,16 +485,20 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading KProcess currentProcess = _system.Scheduler.GetCurrentProcess(); + if (!currentProcess.CpuMemory.IsMapped(address)) + { + _system.CriticalSection.Leave(); + + return KernelResult.InvalidMemState; + } + + ref int valueRef = ref currentProcess.CpuMemory.GetRef(address); + int currentValue; do { - if (!KernelTransfer.UserToKernelInt32(_system, address, out currentValue)) - { - _system.CriticalSection.Leave(); - - return KernelResult.InvalidMemState; - } + currentValue = valueRef; if (currentValue != value) { @@ -498,7 +507,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading return KernelResult.InvalidState; } } - while (!currentProcess.CpuMemory.AtomicCompareExchangeInt32((long)address, currentValue, currentValue + 1)); + while (Interlocked.CompareExchange(ref valueRef, currentValue + 1, currentValue) != currentValue); WakeArbiterThreads(address, count); @@ -537,16 +546,20 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading KProcess currentProcess = _system.Scheduler.GetCurrentProcess(); + if (!currentProcess.CpuMemory.IsMapped(address)) + { + _system.CriticalSection.Leave(); + + return KernelResult.InvalidMemState; + } + + ref int valueRef = ref currentProcess.CpuMemory.GetRef(address); + int currentValue; do { - if (!KernelTransfer.UserToKernelInt32(_system, address, out currentValue)) - { - _system.CriticalSection.Leave(); - - return KernelResult.InvalidMemState; - } + currentValue = valueRef; if (currentValue != value) { @@ -555,7 +568,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading return KernelResult.InvalidState; } } - while (!currentProcess.CpuMemory.AtomicCompareExchangeInt32((long)address, currentValue, currentValue + offset)); + while (Interlocked.CompareExchange(ref valueRef, currentValue + offset, currentValue) != currentValue); WakeArbiterThreads(address, count); diff --git a/Ryujinx.HLE/HOS/Kernel/Threading/KThread.cs b/Ryujinx.HLE/HOS/Kernel/Threading/KThread.cs index e629be45..24c4b2a3 100644 --- a/Ryujinx.HLE/HOS/Kernel/Threading/KThread.cs +++ b/Ryujinx.HLE/HOS/Kernel/Threading/KThread.cs @@ -1,5 +1,5 @@ -using ARMeilleure.Memory; using Ryujinx.Common.Logging; +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Process; using System; @@ -163,7 +163,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading HostThread = new Thread(customHostThreadStart ?? (() => ThreadStart(entrypoint))); - Context = new ARMeilleure.State.ExecutionContext(); + Context = CpuContext.CreateExecutionContext(); bool isAarch32 = (Owner.MmuFlags & 1) == 0; @@ -1141,7 +1141,7 @@ namespace Ryujinx.HLE.HOS.Kernel.Threading private void ThreadStart(ulong entrypoint) { - Owner.Translator.Execute(Context, entrypoint); + Owner.CpuContext.Execute(Context, entrypoint); ThreadExit(); diff --git a/Ryujinx.HLE/HOS/ProgramLoader.cs b/Ryujinx.HLE/HOS/ProgramLoader.cs index 044bc9c6..3dd7d703 100644 --- a/Ryujinx.HLE/HOS/ProgramLoader.cs +++ b/Ryujinx.HLE/HOS/ProgramLoader.cs @@ -1,7 +1,6 @@ -using ARMeilleure.Memory; -using LibHac; using Ryujinx.Common; using Ryujinx.Common.Logging; +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Memory; using Ryujinx.HLE.HOS.Kernel.Process; @@ -270,9 +269,9 @@ namespace Ryujinx.HLE.HOS end = bssStart + (ulong)image.BssSize; } - process.CpuMemory.WriteBytes((long)textStart, image.Text); - process.CpuMemory.WriteBytes((long)roStart, image.Ro); - process.CpuMemory.WriteBytes((long)dataStart, image.Data); + process.CpuMemory.Write(textStart, image.Text); + process.CpuMemory.Write(roStart, image.Ro); + process.CpuMemory.Write(dataStart, image.Data); MemoryHelper.FillWithZeros(process.CpuMemory, (long)bssStart, image.BssSize); diff --git a/Ryujinx.HLE/HOS/ServiceCtx.cs b/Ryujinx.HLE/HOS/ServiceCtx.cs index fa6cfca3..533c1d67 100644 --- a/Ryujinx.HLE/HOS/ServiceCtx.cs +++ b/Ryujinx.HLE/HOS/ServiceCtx.cs @@ -1,4 +1,4 @@ -using ARMeilleure.Memory; +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Ipc; using Ryujinx.HLE.HOS.Kernel.Ipc; using Ryujinx.HLE.HOS.Kernel.Process; diff --git a/Ryujinx.HLE/HOS/Services/Account/Acc/IAccountServiceForApplication.cs b/Ryujinx.HLE/HOS/Services/Account/Acc/IAccountServiceForApplication.cs index a1c2c836..3709db3a 100644 --- a/Ryujinx.HLE/HOS/Services/Account/Acc/IAccountServiceForApplication.cs +++ b/Ryujinx.HLE/HOS/Services/Account/Acc/IAccountServiceForApplication.cs @@ -1,6 +1,6 @@ -using ARMeilleure.Memory; using Ryujinx.Common; using Ryujinx.Common.Logging; +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Services.Arp; using System.Collections.Generic; @@ -75,8 +75,8 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc break; } - context.Memory.WriteInt64(outputPosition + (long)offset, userProfile.UserId.High); - context.Memory.WriteInt64(outputPosition + (long)offset + 8, userProfile.UserId.Low); + context.Memory.Write((ulong)outputPosition + offset, userProfile.UserId.High); + context.Memory.Write((ulong)outputPosition + offset + 8, userProfile.UserId.Low); offset += 0x10; } @@ -240,7 +240,9 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc return ResultCode.InvalidInputBufferSize; } - byte[] thumbnailBuffer = context.Memory.ReadBytes(inputPosition, inputSize); + byte[] thumbnailBuffer = new byte[inputSize]; + + context.Memory.Read((ulong)inputPosition, thumbnailBuffer); // TODO: Store thumbnailBuffer somewhere, in save data 0x8000000000000010 ? diff --git a/Ryujinx.HLE/HOS/Services/Account/Acc/IProfile.cs b/Ryujinx.HLE/HOS/Services/Account/Acc/IProfile.cs index 0470832b..24a76e50 100644 --- a/Ryujinx.HLE/HOS/Services/Account/Acc/IProfile.cs +++ b/Ryujinx.HLE/HOS/Services/Account/Acc/IProfile.cs @@ -1,5 +1,5 @@ -using ARMeilleure.Memory; using Ryujinx.Common.Logging; +using Ryujinx.Cpu; using Ryujinx.HLE.Utilities; using System.IO; using System.Reflection; @@ -28,9 +28,9 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc MemoryHelper.FillWithZeros(context.Memory, position, 0x80); - context.Memory.WriteInt32(position, 0); - context.Memory.WriteInt32(position + 4, 1); - context.Memory.WriteInt64(position + 8, 1); + context.Memory.Write((ulong)position, 0); + context.Memory.Write((ulong)position + 4, 1); + context.Memory.Write((ulong)position + 8, 1L); return GetBase(context); } @@ -70,7 +70,7 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc _profilePictureStream.Read(profilePictureData, 0, profilePictureData.Length); - context.Memory.WriteBytes(bufferPosition, profilePictureData); + context.Memory.Write((ulong)bufferPosition, profilePictureData); context.ResponseData.Write(_profilePictureStream.Length); diff --git a/Ryujinx.HLE/HOS/Services/Am/AppletAE/IStorageAccessor.cs b/Ryujinx.HLE/HOS/Services/Am/AppletAE/IStorageAccessor.cs index 5013e2e3..ddd97a4c 100644 --- a/Ryujinx.HLE/HOS/Services/Am/AppletAE/IStorageAccessor.cs +++ b/Ryujinx.HLE/HOS/Services/Am/AppletAE/IStorageAccessor.cs @@ -44,7 +44,9 @@ namespace Ryujinx.HLE.HOS.Services.Am.AppletAE size = maxSize; } - byte[] data = context.Memory.ReadBytes(position, size); + byte[] data = new byte[size]; + + context.Memory.Read((ulong)position, data); Buffer.BlockCopy(data, 0, _storage.Data, (int)writePosition, (int)size); } @@ -71,7 +73,7 @@ namespace Ryujinx.HLE.HOS.Services.Am.AppletAE Buffer.BlockCopy(_storage.Data, (int)readPosition, data, 0, (int)size); - context.Memory.WriteBytes(position, data); + context.Memory.Write((ulong)position, data); return ResultCode.Success; } diff --git a/Ryujinx.HLE/HOS/Services/Audio/AudioOutManager/IAudioOut.cs b/Ryujinx.HLE/HOS/Services/Audio/AudioOutManager/IAudioOut.cs index 11d8036c..e6b7cb3d 100644 --- a/Ryujinx.HLE/HOS/Services/Audio/AudioOutManager/IAudioOut.cs +++ b/Ryujinx.HLE/HOS/Services/Audio/AudioOutManager/IAudioOut.cs @@ -1,5 +1,5 @@ -using ARMeilleure.Memory; using Ryujinx.Audio; +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Ipc; using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Threading; @@ -106,9 +106,9 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioOutManager context.Memory, position); - byte[] buffer = context.Memory.ReadBytes( - data.SampleBufferPtr, - data.SampleBufferSize); + byte[] buffer = new byte[data.SampleBufferSize]; + + context.Memory.Read((ulong)data.SampleBufferPtr, buffer); _audioOut.AppendBuffer(_track, tag, buffer); @@ -139,7 +139,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioOutManager tag = releasedBuffers[index]; } - context.Memory.WriteInt64(position + index * 8, tag); + context.Memory.Write((ulong)(position + index * 8), tag); } context.ResponseData.Write(releasedBuffers.Length); diff --git a/Ryujinx.HLE/HOS/Services/Audio/AudioRendererManager/IAudioDevice.cs b/Ryujinx.HLE/HOS/Services/Audio/AudioRendererManager/IAudioDevice.cs index ab4aee76..89c064e3 100644 --- a/Ryujinx.HLE/HOS/Services/Audio/AudioRendererManager/IAudioDevice.cs +++ b/Ryujinx.HLE/HOS/Services/Audio/AudioRendererManager/IAudioDevice.cs @@ -44,7 +44,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager break; } - context.Memory.WriteBytes(position, buffer); + context.Memory.Write((ulong)position, buffer); position += buffer.Length; } @@ -61,7 +61,9 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager long position = context.Request.SendBuff[0].Position; long size = context.Request.SendBuff[0].Size; - byte[] deviceNameBuffer = context.Memory.ReadBytes(position, size); + byte[] deviceNameBuffer = new byte[size]; + + context.Memory.Read((ulong)position, deviceNameBuffer); string deviceName = Encoding.ASCII.GetString(deviceNameBuffer); @@ -83,7 +85,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager if ((ulong)deviceNameBuffer.Length <= (ulong)size) { - context.Memory.WriteBytes(position, deviceNameBuffer); + context.Memory.Write((ulong)position, deviceNameBuffer); } else { @@ -143,7 +145,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager break; } - context.Memory.WriteBytes(position, buffer); + context.Memory.Write((ulong)position, buffer); position += buffer.Length; } @@ -159,7 +161,9 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager (long position, long size) = context.Request.GetBufferType0x21(); - byte[] deviceNameBuffer = context.Memory.ReadBytes(position, size); + byte[] deviceNameBuffer = new byte[size]; + + context.Memory.Read((ulong)position, deviceNameBuffer); string deviceName = Encoding.UTF8.GetString(deviceNameBuffer); @@ -191,7 +195,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager if ((ulong)deviceNameBuffer.Length <= (ulong)size) { - context.Memory.WriteBytes(position, deviceNameBuffer); + context.Memory.Write((ulong)position, deviceNameBuffer); } else { diff --git a/Ryujinx.HLE/HOS/Services/Audio/AudioRendererManager/IAudioRenderer.cs b/Ryujinx.HLE/HOS/Services/Audio/AudioRendererManager/IAudioRenderer.cs index b9f1d8df..e899f945 100644 --- a/Ryujinx.HLE/HOS/Services/Audio/AudioRendererManager/IAudioRenderer.cs +++ b/Ryujinx.HLE/HOS/Services/Audio/AudioRendererManager/IAudioRenderer.cs @@ -1,7 +1,7 @@ -using ARMeilleure.Memory; using Ryujinx.Audio; using Ryujinx.Audio.Adpcm; using Ryujinx.Common.Logging; +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Ipc; using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Threading; @@ -333,7 +333,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager for (int offset = 0; offset < size; offset += 2) { - context.Coefficients[offset >> 1] = _memory.ReadInt16(position + offset); + context.Coefficients[offset >> 1] = _memory.Read((ulong)(position + offset)); } return context; diff --git a/Ryujinx.HLE/HOS/Services/Audio/AudioRendererManager/VoiceContext.cs b/Ryujinx.HLE/HOS/Services/Audio/AudioRendererManager/VoiceContext.cs index 4af04ee0..c14c46b3 100644 --- a/Ryujinx.HLE/HOS/Services/Audio/AudioRendererManager/VoiceContext.cs +++ b/Ryujinx.HLE/HOS/Services/Audio/AudioRendererManager/VoiceContext.cs @@ -1,5 +1,5 @@ -using ARMeilleure.Memory; using Ryujinx.Audio.Adpcm; +using Ryujinx.Cpu; using System; namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager @@ -146,7 +146,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager { for (int index = 0; index < samplesCount; index++) { - short sample = memory.ReadInt16(wb.Position + index * 2); + short sample = memory.Read((ulong)(wb.Position + index * 2)); _samples[index * 2 + 0] = sample; _samples[index * 2 + 1] = sample; @@ -156,13 +156,15 @@ namespace Ryujinx.HLE.HOS.Services.Audio.AudioRendererManager { for (int index = 0; index < samplesCount * 2; index++) { - _samples[index] = memory.ReadInt16(wb.Position + index * 2); + _samples[index] = memory.Read((ulong)(wb.Position + index * 2)); } } } else if (SampleFormat == SampleFormat.Adpcm) { - byte[] buffer = memory.ReadBytes(wb.Position, wb.Size); + byte[] buffer = new byte[wb.Size]; + + memory.Read((ulong)wb.Position, buffer); _samples = AdpcmDecoder.Decode(buffer, AdpcmCtx); } diff --git a/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IHardwareOpusDecoder.cs b/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IHardwareOpusDecoder.cs index 079f2ae7..e8fcc4b6 100644 --- a/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IHardwareOpusDecoder.cs +++ b/Ryujinx.HLE/HOS/Services/Audio/HardwareOpusDecoderManager/IHardwareOpusDecoder.cs @@ -111,7 +111,11 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager long outputPosition = context.Request.ReceiveBuff[0].Position; long outputSize = context.Request.ReceiveBuff[0].Size; - using (BinaryReader inputStream = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(inPosition, inSize)))) + byte[] buffer = new byte[inSize]; + + context.Memory.Read((ulong)inPosition, buffer); + + using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer))) { result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples); @@ -119,7 +123,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager { byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)]; Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length); - context.Memory.WriteBytes(outputPosition, pcmDataBytes); + context.Memory.Write((ulong)outputPosition, pcmDataBytes); context.ResponseData.Write(outConsumed); context.ResponseData.Write(outSamples); @@ -140,7 +144,11 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager long outputPosition = context.Request.ReceiveBuff[0].Position; long outputSize = context.Request.ReceiveBuff[0].Size; - using (BinaryReader inputStream = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(inPosition, inSize)))) + byte[] buffer = new byte[inSize]; + + context.Memory.Read((ulong)inPosition, buffer); + + using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer))) { result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples); @@ -148,7 +156,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager { byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)]; Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length); - context.Memory.WriteBytes(outputPosition, pcmDataBytes); + context.Memory.Write((ulong)outputPosition, pcmDataBytes); context.ResponseData.Write(outConsumed); context.ResponseData.Write(outSamples); @@ -174,7 +182,11 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager long outputPosition = context.Request.ReceiveBuff[0].Position; long outputSize = context.Request.ReceiveBuff[0].Size; - using (BinaryReader inputStream = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(inPosition, inSize)))) + byte[] buffer = new byte[inSize]; + + context.Memory.Read((ulong)inPosition, buffer); + + using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer))) { result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples); @@ -182,7 +194,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager { byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)]; Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length); - context.Memory.WriteBytes(outputPosition, pcmDataBytes); + context.Memory.Write((ulong)outputPosition, pcmDataBytes); context.ResponseData.Write(outConsumed); context.ResponseData.Write(outSamples); @@ -208,7 +220,11 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager long outputPosition = context.Request.ReceiveBuff[0].Position; long outputSize = context.Request.ReceiveBuff[0].Size; - using (BinaryReader inputStream = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(inPosition, inSize)))) + byte[] buffer = new byte[inSize]; + + context.Memory.Read((ulong)inPosition, buffer); + + using (BinaryReader inputStream = new BinaryReader(new MemoryStream(buffer))) { result = DecodeInterleavedInternal(inputStream, out short[] outPcmData, outputSize, out uint outConsumed, out int outSamples); @@ -216,7 +232,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio.HardwareOpusDecoderManager { byte[] pcmDataBytes = new byte[outPcmData.Length * sizeof(short)]; Buffer.BlockCopy(outPcmData, 0, pcmDataBytes, 0, pcmDataBytes.Length); - context.Memory.WriteBytes(outputPosition, pcmDataBytes); + context.Memory.Write((ulong)outputPosition, pcmDataBytes); context.ResponseData.Write(outConsumed); context.ResponseData.Write(outSamples); diff --git a/Ryujinx.HLE/HOS/Services/Audio/IAudioOutManager.cs b/Ryujinx.HLE/HOS/Services/Audio/IAudioOutManager.cs index 19ee8067..370b2302 100644 --- a/Ryujinx.HLE/HOS/Services/Audio/IAudioOutManager.cs +++ b/Ryujinx.HLE/HOS/Services/Audio/IAudioOutManager.cs @@ -1,6 +1,6 @@ -using ARMeilleure.Memory; using Ryujinx.Audio; using Ryujinx.Common.Logging; +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Kernel.Threading; using Ryujinx.HLE.HOS.Services.Audio.AudioOutManager; using System.Text; @@ -72,7 +72,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio if ((ulong)deviceNameBuffer.Length <= (ulong)size) { - context.Memory.WriteBytes(position, deviceNameBuffer); + context.Memory.Write((ulong)position, deviceNameBuffer); nameCount++; } @@ -109,7 +109,7 @@ namespace Ryujinx.HLE.HOS.Services.Audio if ((ulong)deviceNameBuffer.Length <= (ulong)receiveSize) { - context.Memory.WriteBytes(receivePosition, deviceNameBuffer); + context.Memory.Write((ulong)receivePosition, deviceNameBuffer); } else { diff --git a/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheDirectoryService.cs b/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheDirectoryService.cs index ce5c575e..b735b30b 100644 --- a/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheDirectoryService.cs +++ b/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheDirectoryService.cs @@ -37,7 +37,7 @@ namespace Ryujinx.HLE.HOS.Services.Bcat.ServiceCreator Result result = _base.Read(out int entriesRead, MemoryMarshal.Cast(data)); - context.Memory.WriteBytes(position, data); + context.Memory.Write((ulong)position, data); context.ResponseData.Write(entriesRead); diff --git a/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheFileService.cs b/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheFileService.cs index a621283f..d8bcb044 100644 --- a/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheFileService.cs +++ b/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheFileService.cs @@ -39,7 +39,7 @@ namespace Ryujinx.HLE.HOS.Services.Bcat.ServiceCreator Result result = _base.Read(out long bytesRead, offset, data); - context.Memory.WriteBytes(position, data); + context.Memory.Write((ulong)position, data); context.ResponseData.Write(bytesRead); diff --git a/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheProgressService.cs b/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheProgressService.cs index cb809048..9ffe9e99 100644 --- a/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheProgressService.cs +++ b/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheProgressService.cs @@ -57,7 +57,7 @@ namespace Ryujinx.HLE.HOS.Services.Bcat.ServiceCreator using (BinaryWriter bufferWriter = new BinaryWriter(memory)) { bufferWriter.WriteStruct(deliveryCacheProgress); - context.Memory.WriteBytes(ipcDesc.Position, memory.ToArray()); + context.Memory.Write((ulong)ipcDesc.Position, memory.ToArray()); } } } diff --git a/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheStorageService.cs b/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheStorageService.cs index 344eb54e..161cc07d 100644 --- a/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheStorageService.cs +++ b/Ryujinx.HLE/HOS/Services/Bcat/ServiceCreator/IDeliveryCacheStorageService.cs @@ -53,7 +53,7 @@ namespace Ryujinx.HLE.HOS.Services.Bcat.ServiceCreator Result result = _base.EnumerateDeliveryCacheDirectory(out int count, MemoryMarshal.Cast(data)); - context.Memory.WriteBytes(position, data); + context.Memory.Write((ulong)position, data); context.ResponseData.Write(count); diff --git a/Ryujinx.HLE/HOS/Services/Friend/ServiceCreator/IFriendService.cs b/Ryujinx.HLE/HOS/Services/Friend/ServiceCreator/IFriendService.cs index 19edc70e..0b10fa36 100644 --- a/Ryujinx.HLE/HOS/Services/Friend/ServiceCreator/IFriendService.cs +++ b/Ryujinx.HLE/HOS/Services/Friend/ServiceCreator/IFriendService.cs @@ -166,7 +166,9 @@ namespace Ryujinx.HLE.HOS.Services.Friend.ServiceCreator long position = context.Request.PtrBuff[0].Position; long size = context.Request.PtrBuff[0].Size; - byte[] bufferContent = context.Memory.ReadBytes(position, size); + byte[] bufferContent = new byte[size]; + + context.Memory.Read((ulong)position, bufferContent); if (uuid.IsNull) { diff --git a/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/FileSystemProxyHelper.cs b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/FileSystemProxyHelper.cs index 46151cbc..525dff60 100644 --- a/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/FileSystemProxyHelper.cs +++ b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/FileSystemProxyHelper.cs @@ -119,7 +119,9 @@ namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy long position = context.Request.PtrBuff[index].Position; long size = context.Request.PtrBuff[index].Size; - byte[] pathBytes = context.Memory.ReadBytes(position, size); + byte[] pathBytes = new byte[size]; + + context.Memory.Read((ulong)position, pathBytes); return FsPath.FromSpan(out path, pathBytes); } diff --git a/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IDirectory.cs b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IDirectory.cs index c042ed8e..3d07b509 100644 --- a/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IDirectory.cs +++ b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IDirectory.cs @@ -26,7 +26,7 @@ namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy Result result = _baseDirectory.Read(out long entriesRead, entries); - context.Memory.WriteBytes(bufferPosition, entriesBytes); + context.Memory.Write((ulong)bufferPosition, entriesBytes); context.ResponseData.Write(entriesRead); return (ResultCode)result.Value; diff --git a/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFile.cs b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFile.cs index f09624f8..02fc967f 100644 --- a/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFile.cs +++ b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IFile.cs @@ -29,7 +29,7 @@ namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy Result result = _baseFile.Read(out long bytesRead, offset, data, readOption); - context.Memory.WriteBytes(position, data); + context.Memory.Write((ulong)position, data); context.ResponseData.Write(bytesRead); @@ -48,7 +48,9 @@ namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy long offset = context.RequestData.ReadInt64(); long size = context.RequestData.ReadInt64(); - byte[] data = context.Memory.ReadBytes(position, size); + byte[] data = new byte[size]; + + context.Memory.Read((ulong)position, data); return (ResultCode)_baseFile.Write(offset, data, writeOption).Value; } diff --git a/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IStorage.cs b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IStorage.cs index b2db08dd..24e9019a 100644 --- a/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IStorage.cs +++ b/Ryujinx.HLE/HOS/Services/Fs/FileSystemProxy/IStorage.cs @@ -34,7 +34,7 @@ namespace Ryujinx.HLE.HOS.Services.Fs.FileSystemProxy Result result = _baseStorage.Read(offset, data); - context.Memory.WriteBytes(buffDesc.Position, data); + context.Memory.Write((ulong)buffDesc.Position, data); return (ResultCode)result.Value; } diff --git a/Ryujinx.HLE/HOS/Services/Fs/IFileSystemProxy.cs b/Ryujinx.HLE/HOS/Services/Fs/IFileSystemProxy.cs index 537882f1..35cf160f 100644 --- a/Ryujinx.HLE/HOS/Services/Fs/IFileSystemProxy.cs +++ b/Ryujinx.HLE/HOS/Services/Fs/IFileSystemProxy.cs @@ -333,7 +333,7 @@ namespace Ryujinx.HLE.HOS.Services.Fs Result result = _baseFileSystemProxy.FindSaveDataWithFilter(out long count, infoBuffer, spaceId, ref filter); - context.Memory.WriteBytes(bufferPosition, infoBuffer); + context.Memory.Write((ulong)bufferPosition, infoBuffer); context.ResponseData.Write(count); return (ResultCode)result.Value; diff --git a/Ryujinx.HLE/HOS/Services/Fs/ISaveDataInfoReader.cs b/Ryujinx.HLE/HOS/Services/Fs/ISaveDataInfoReader.cs index 3d5ae8e2..a76a7412 100644 --- a/Ryujinx.HLE/HOS/Services/Fs/ISaveDataInfoReader.cs +++ b/Ryujinx.HLE/HOS/Services/Fs/ISaveDataInfoReader.cs @@ -22,7 +22,7 @@ namespace Ryujinx.HLE.HOS.Services.Fs Result result = _baseReader.ReadSaveDataInfo(out long readCount, infoBuffer); - context.Memory.WriteBytes(bufferPosition, infoBuffer); + context.Memory.Write((ulong)bufferPosition, infoBuffer); context.ResponseData.Write(readCount); return (ResultCode)result.Value; diff --git a/Ryujinx.HLE/HOS/Services/Hid/Hid.cs b/Ryujinx.HLE/HOS/Services/Hid/Hid.cs index c4935a64..c355a050 100644 --- a/Ryujinx.HLE/HOS/Services/Hid/Hid.cs +++ b/Ryujinx.HLE/HOS/Services/Hid/Hid.cs @@ -7,9 +7,11 @@ namespace Ryujinx.HLE.HOS.Services.Hid public class Hid { private readonly Switch _device; - private readonly long _hidMemoryAddress; - internal ref HidSharedMemory SharedMemory => ref _device.Memory.GetStructRef(_hidMemoryAddress); + private readonly ulong _hidMemoryAddress; + + internal ref HidSharedMemory SharedMemory => ref _device.Memory.GetRef(_hidMemoryAddress); + internal const int SharedMemEntryCount = 17; public DebugPadDevice DebugPad; @@ -46,12 +48,12 @@ namespace Ryujinx.HLE.HOS.Services.Hid } } - public Hid(in Switch device, long sharedHidMemoryAddress) + public Hid(in Switch device, ulong sharedHidMemoryAddress) { _device = device; _hidMemoryAddress = sharedHidMemoryAddress; - device.Memory.FillWithZeros(sharedHidMemoryAddress, Horizon.HidSize); + device.Memory.ZeroFill(sharedHidMemoryAddress, Horizon.HidSize); } public void InitDevices() diff --git a/Ryujinx.HLE/HOS/Services/Hid/IHidServer.cs b/Ryujinx.HLE/HOS/Services/Hid/IHidServer.cs index 5e84eda6..ec4a59df 100644 --- a/Ryujinx.HLE/HOS/Services/Hid/IHidServer.cs +++ b/Ryujinx.HLE/HOS/Services/Hid/IHidServer.cs @@ -580,7 +580,7 @@ namespace Ryujinx.HLE.HOS.Services.Hid for (int i = 0; i < arraySize; ++i) { - supportedPlayerIds[i] = (NpadIdType)context.Memory.ReadInt32(context.Request.PtrBuff[0].Position + i * 4); + supportedPlayerIds[i] = context.Memory.Read((ulong)(context.Request.PtrBuff[0].Position + i * 4)); } Logger.PrintStub(LogClass.ServiceHid, $"{arraySize} " + string.Join(",", supportedPlayerIds)); @@ -980,13 +980,13 @@ namespace Ryujinx.HLE.HOS.Services.Hid { long appletResourceUserId = context.RequestData.ReadInt64(); - byte[] vibrationDeviceHandleBuffer = context.Memory.ReadBytes( - context.Request.PtrBuff[0].Position, - context.Request.PtrBuff[0].Size); + byte[] vibrationDeviceHandleBuffer = new byte[context.Request.PtrBuff[0].Size]; - byte[] vibrationValueBuffer = context.Memory.ReadBytes( - context.Request.PtrBuff[1].Position, - context.Request.PtrBuff[1].Size); + context.Memory.Read((ulong)context.Request.PtrBuff[0].Position, vibrationDeviceHandleBuffer); + + byte[] vibrationValueBuffer = new byte[context.Request.PtrBuff[1].Size]; + + context.Memory.Read((ulong)context.Request.PtrBuff[1].Position, vibrationValueBuffer); // TODO: Read all handles and values from buffer. diff --git a/Ryujinx.HLE/HOS/Services/Lm/LogService/ILogger.cs b/Ryujinx.HLE/HOS/Services/Lm/LogService/ILogger.cs index a269312e..05b7238e 100644 --- a/Ryujinx.HLE/HOS/Services/Lm/LogService/ILogger.cs +++ b/Ryujinx.HLE/HOS/Services/Lm/LogService/ILogger.cs @@ -33,7 +33,10 @@ namespace Ryujinx.HLE.HOS.Services.Lm.LogService public ResultCode Log(ServiceCtx context) { (long bufPos, long bufSize) = context.Request.GetBufferType0x21(); - byte[] logBuffer = context.Memory.ReadBytes(bufPos, bufSize); + + byte[] logBuffer = new byte[bufSize]; + + context.Memory.Read((ulong)bufPos, logBuffer); using (MemoryStream ms = new MemoryStream(logBuffer)) { diff --git a/Ryujinx.HLE/HOS/Services/Mii/StaticService/IDatabaseService.cs b/Ryujinx.HLE/HOS/Services/Mii/StaticService/IDatabaseService.cs index 4ce531f4..e4a29f59 100644 --- a/Ryujinx.HLE/HOS/Services/Mii/StaticService/IDatabaseService.cs +++ b/Ryujinx.HLE/HOS/Services/Mii/StaticService/IDatabaseService.cs @@ -261,7 +261,7 @@ namespace Ryujinx.HLE.HOS.Services.Mii.StaticService ResultCode result = Export(data); - context.Memory.WriteBytes(outputBuffer.Position, data.ToArray()); + context.Memory.Write((ulong)outputBuffer.Position, data.ToArray()); return result; } @@ -350,7 +350,9 @@ namespace Ryujinx.HLE.HOS.Services.Mii.StaticService } else { - rawData = context.Memory.ReadBytes(ipcBuff.Position, ipcBuff.Size); + rawData = new byte[ipcBuff.Size]; + + context.Memory.Read((ulong)ipcBuff.Position, rawData); } return new Span(rawData); @@ -365,7 +367,7 @@ namespace Ryujinx.HLE.HOS.Services.Mii.StaticService { Span rawData = MemoryMarshal.Cast(span); - context.Memory.WriteBytes(ipcBuff.Position, rawData.ToArray()); + context.Memory.Write((ulong)ipcBuff.Position, rawData); } protected abstract bool IsUpdated(SourceFlag flag); diff --git a/Ryujinx.HLE/HOS/Services/Ncm/Lr/LocationResolverManager/ILocationResolver.cs b/Ryujinx.HLE/HOS/Services/Ncm/Lr/LocationResolverManager/ILocationResolver.cs index 88ba45e0..25d6f548 100644 --- a/Ryujinx.HLE/HOS/Services/Ncm/Lr/LocationResolverManager/ILocationResolver.cs +++ b/Ryujinx.HLE/HOS/Services/Ncm/Lr/LocationResolverManager/ILocationResolver.cs @@ -233,7 +233,7 @@ namespace Ryujinx.HLE.HOS.Services.Ncm.Lr.LocationResolverManager byte[] contentPathBuffer = Encoding.UTF8.GetBytes(contentPath); - context.Memory.WriteBytes(position, contentPathBuffer); + context.Memory.Write((ulong)position, contentPathBuffer); } else { diff --git a/Ryujinx.HLE/HOS/Services/Nfc/Nfp/UserManager/IUser.cs b/Ryujinx.HLE/HOS/Services/Nfc/Nfp/UserManager/IUser.cs index 3f6518e5..91ed9391 100644 --- a/Ryujinx.HLE/HOS/Services/Nfc/Nfp/UserManager/IUser.cs +++ b/Ryujinx.HLE/HOS/Services/Nfc/Nfp/UserManager/IUser.cs @@ -31,7 +31,9 @@ namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp long inputPosition = context.Request.SendBuff[0].Position; long inputSize = context.Request.SendBuff[0].Size; - byte[] unknownBuffer = context.Memory.ReadBytes(inputPosition, inputSize); + byte[] unknownBuffer = new byte[inputSize]; + + context.Memory.Read((ulong)inputPosition, unknownBuffer); // NOTE: appletResourceUserId, mcuVersionData and the buffer are stored inside an internal struct. // The buffer seems to contains entries with a size of 0x40 bytes each. @@ -89,7 +91,7 @@ namespace Ryujinx.HLE.HOS.Services.Nfc.Nfp for (int i = 0; i < _devices.Count; i++) { - context.Memory.WriteUInt32(outputPosition + (i * sizeof(long)), (uint)_devices[i].Handle); + context.Memory.Write((ulong)(outputPosition + (i * sizeof(long))), (uint)_devices[i].Handle); } context.ResponseData.Write(_devices.Count); diff --git a/Ryujinx.HLE/HOS/Services/Nifm/StaticService/IGeneralService.cs b/Ryujinx.HLE/HOS/Services/Nifm/StaticService/IGeneralService.cs index eaa9be49..023a1f3e 100644 --- a/Ryujinx.HLE/HOS/Services/Nifm/StaticService/IGeneralService.cs +++ b/Ryujinx.HLE/HOS/Services/Nifm/StaticService/IGeneralService.cs @@ -34,7 +34,7 @@ namespace Ryujinx.HLE.HOS.Services.Nifm.StaticService long position = context.Request.RecvListBuff[0].Position; long size = context.Request.RecvListBuff[0].Size; - context.Memory.WriteInt32(position, _generalServiceDetail.ClientId); + context.Memory.Write((ulong)position, _generalServiceDetail.ClientId); return ResultCode.Success; } @@ -120,7 +120,7 @@ namespace Ryujinx.HLE.HOS.Services.Nifm.StaticService long position = context.Request.PtrBuff[0].Position; long size = context.Request.PtrBuff[0].Size; - int clientId = context.Memory.ReadInt32(position); + int clientId = context.Memory.Read((ulong)position); context.ResponseData.Write(GeneralServiceManager.Get(clientId).IsAnyInternetRequestAccepted); diff --git a/Ryujinx.HLE/HOS/Services/Ns/IApplicationManagerInterface.cs b/Ryujinx.HLE/HOS/Services/Ns/IApplicationManagerInterface.cs index 3920030e..f6a8530f 100644 --- a/Ryujinx.HLE/HOS/Services/Ns/IApplicationManagerInterface.cs +++ b/Ryujinx.HLE/HOS/Services/Ns/IApplicationManagerInterface.cs @@ -16,7 +16,7 @@ byte[] nacpData = context.Device.System.ControlData.ByteSpan.ToArray(); - context.Memory.WriteBytes(position, nacpData); + context.Memory.Write((ulong)position, nacpData); return ResultCode.Success; } diff --git a/Ryujinx.HLE/HOS/Services/Ns/IReadOnlyApplicationControlDataInterface.cs b/Ryujinx.HLE/HOS/Services/Ns/IReadOnlyApplicationControlDataInterface.cs index a7f20bc7..01aa04a1 100644 --- a/Ryujinx.HLE/HOS/Services/Ns/IReadOnlyApplicationControlDataInterface.cs +++ b/Ryujinx.HLE/HOS/Services/Ns/IReadOnlyApplicationControlDataInterface.cs @@ -15,7 +15,7 @@ byte[] nacpData = context.Device.System.ControlData.ByteSpan.ToArray(); - context.Memory.WriteBytes(position, nacpData); + context.Memory.Write((ulong)position, nacpData); return ResultCode.Success; } diff --git a/Ryujinx.HLE/HOS/Services/Nv/INvDrvServices.cs b/Ryujinx.HLE/HOS/Services/Nv/INvDrvServices.cs index 80a2e4b7..6e45375f 100644 --- a/Ryujinx.HLE/HOS/Services/Nv/INvDrvServices.cs +++ b/Ryujinx.HLE/HOS/Services/Nv/INvDrvServices.cs @@ -1,6 +1,6 @@ -using ARMeilleure.Memory; using Ryujinx.Common; using Ryujinx.Common.Logging; +using Ryujinx.Cpu; using Ryujinx.HLE.Exceptions; using Ryujinx.HLE.HOS.Ipc; using Ryujinx.HLE.HOS.Kernel.Memory; @@ -102,7 +102,9 @@ namespace Ryujinx.HLE.HOS.Services.Nv byte[] outputData = new byte[outputDataSize]; - byte[] temp = context.Memory.ReadBytes(inputDataPosition, inputDataSize); + byte[] temp = new byte[inputDataSize]; + + context.Memory.Read((ulong)inputDataPosition, temp); Buffer.BlockCopy(temp, 0, outputData, 0, temp.Length); @@ -116,7 +118,11 @@ namespace Ryujinx.HLE.HOS.Services.Nv } else { - arguments = new Span(context.Memory.ReadBytes(inputDataPosition, inputDataSize)); + byte[] temp = new byte[inputDataSize]; + + context.Memory.Read((ulong)inputDataPosition, temp); + + arguments = new Span(temp); } return NvResult.Success; @@ -266,7 +272,7 @@ namespace Ryujinx.HLE.HOS.Services.Nv if ((ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0) { - context.Memory.WriteBytes(context.Request.GetBufferType0x22(0).Position, arguments.ToArray()); + context.Memory.Write((ulong)context.Request.GetBufferType0x22(0).Position, arguments.ToArray()); } } } @@ -435,7 +441,11 @@ namespace Ryujinx.HLE.HOS.Services.Nv errorCode = GetIoctlArgument(context, ioctlCommand, out Span arguments); - Span inlineInBuffer = new Span(context.Memory.ReadBytes(inlineInBufferPosition, inlineInBufferSize)); + byte[] temp = new byte[inlineInBufferSize]; + + context.Memory.Read((ulong)inlineInBufferPosition, temp); + + Span inlineInBuffer = new Span(temp); if (errorCode == NvResult.Success) { @@ -454,7 +464,7 @@ namespace Ryujinx.HLE.HOS.Services.Nv if ((ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0) { - context.Memory.WriteBytes(context.Request.GetBufferType0x22(0).Position, arguments.ToArray()); + context.Memory.Write((ulong)context.Request.GetBufferType0x22(0).Position, arguments.ToArray()); } } } @@ -480,7 +490,11 @@ namespace Ryujinx.HLE.HOS.Services.Nv errorCode = GetIoctlArgument(context, ioctlCommand, out Span arguments); - Span inlineOutBuffer = new Span(context.Memory.ReadBytes(inlineOutBufferPosition, inlineOutBufferSize)); + byte[] temp = new byte[inlineOutBufferSize]; + + context.Memory.Read((ulong)inlineOutBufferPosition, temp); + + Span inlineOutBuffer = new Span(temp); if (errorCode == NvResult.Success) { @@ -499,8 +513,8 @@ namespace Ryujinx.HLE.HOS.Services.Nv if ((ioctlCommand.DirectionValue & NvIoctl.Direction.Write) != 0) { - context.Memory.WriteBytes(context.Request.GetBufferType0x22(0).Position, arguments.ToArray()); - context.Memory.WriteBytes(inlineOutBufferPosition, inlineOutBuffer.ToArray()); + context.Memory.Write((ulong)context.Request.GetBufferType0x22(0).Position, arguments.ToArray()); + context.Memory.Write((ulong)inlineOutBufferPosition, inlineOutBuffer.ToArray()); } } } diff --git a/Ryujinx.HLE/HOS/Services/Nv/NvDrvServices/NvHostChannel/NvHostChannelDeviceFile.cs b/Ryujinx.HLE/HOS/Services/Nv/NvDrvServices/NvHostChannel/NvHostChannelDeviceFile.cs index 573df48c..208bec3b 100644 --- a/Ryujinx.HLE/HOS/Services/Nv/NvDrvServices/NvHostChannel/NvHostChannelDeviceFile.cs +++ b/Ryujinx.HLE/HOS/Services/Nv/NvDrvServices/NvHostChannel/NvHostChannelDeviceFile.cs @@ -21,7 +21,7 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel private Switch _device; - private ARMeilleure.Memory.MemoryManager _memory; + private Cpu.MemoryManager _memory; public enum ResourcePolicy { @@ -143,7 +143,7 @@ namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvHostChannel for (int offset = 0; offset < commandBufferData.Length; offset++) { - commandBufferData[offset] = _memory.ReadInt32(map.Address + commandBufferEntry.Offset + offset * 4); + commandBufferData[offset] = _memory.Read((ulong)(map.Address + commandBufferEntry.Offset + offset * 4)); } // TODO: Submit command to engines. diff --git a/Ryujinx.HLE/HOS/Services/Prepo/IPrepoService.cs b/Ryujinx.HLE/HOS/Services/Prepo/IPrepoService.cs index 32a5950d..966e4596 100644 --- a/Ryujinx.HLE/HOS/Services/Prepo/IPrepoService.cs +++ b/Ryujinx.HLE/HOS/Services/Prepo/IPrepoService.cs @@ -95,7 +95,9 @@ namespace Ryujinx.HLE.HOS.Services.Prepo return ResultCode.InvalidBufferSize; } - byte[] inputBuffer = context.Memory.ReadBytes(inputPosition, inputSize); + byte[] inputBuffer = new byte[inputSize]; + + context.Memory.Read((ulong)inputPosition, inputBuffer); Logger.PrintInfo(LogClass.ServicePrepo, ReadReportBuffer(inputBuffer, gameRoom, userId)); diff --git a/Ryujinx.HLE/HOS/Services/Ro/IRoInterface.cs b/Ryujinx.HLE/HOS/Services/Ro/IRoInterface.cs index e8bebb8a..3ff622b6 100644 --- a/Ryujinx.HLE/HOS/Services/Ro/IRoInterface.cs +++ b/Ryujinx.HLE/HOS/Services/Ro/IRoInterface.cs @@ -1,6 +1,5 @@ -using ARMeilleure.Memory; -using Ryujinx.Common; -using Ryujinx.Common.Logging; +using Ryujinx.Common; +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Memory; using Ryujinx.HLE.HOS.Kernel.Process; @@ -69,7 +68,11 @@ namespace Ryujinx.HLE.HOS.Services.Ro for (int i = 0; i < header.HashCount; i++) { - hashes.Add(context.Memory.ReadBytes(nrrAddress + header.HashOffset + (i * 0x20), 0x20)); + byte[] temp = new byte[0x20]; + + context.Memory.Read((ulong)(nrrAddress + header.HashOffset + (i * 0x20)), temp); + + hashes.Add(temp); } nrrInfo = new NrrInfo(nrrAddress, header, hashes); @@ -127,15 +130,18 @@ namespace Ryujinx.HLE.HOS.Services.Ro return ResultCode.InvalidAddress; } - uint magic = context.Memory.ReadUInt32((long)nroAddress + 0x10); - uint nroFileSize = context.Memory.ReadUInt32((long)nroAddress + 0x18); + uint magic = context.Memory.Read(nroAddress + 0x10); + uint nroFileSize = context.Memory.Read(nroAddress + 0x18); if (magic != NroMagic || nroSize != nroFileSize) { return ResultCode.InvalidNro; } - byte[] nroData = context.Memory.ReadBytes((long)nroAddress, (long)nroSize); + byte[] nroData = new byte[nroSize]; + + context.Memory.Read(nroAddress, nroData); + byte[] nroHash = null; MemoryStream stream = new MemoryStream(nroData); @@ -319,9 +325,9 @@ namespace Ryujinx.HLE.HOS.Services.Ro ulong bssEnd = BitUtils.AlignUp(bssStart + (ulong)relocatableObject.BssSize, KMemoryManager.PageSize); - process.CpuMemory.WriteBytes((long)textStart, relocatableObject.Text); - process.CpuMemory.WriteBytes((long)roStart, relocatableObject.Ro); - process.CpuMemory.WriteBytes((long)dataStart, relocatableObject.Data); + process.CpuMemory.Write(textStart, relocatableObject.Text); + process.CpuMemory.Write(roStart, relocatableObject.Ro); + process.CpuMemory.Write(dataStart, relocatableObject.Data); MemoryHelper.FillWithZeros(process.CpuMemory, (long)bssStart, (int)(bssEnd - bssStart)); diff --git a/Ryujinx.HLE/HOS/Services/Sdb/Pdm/QueryService/QueryPlayStatisticsManager.cs b/Ryujinx.HLE/HOS/Services/Sdb/Pdm/QueryService/QueryPlayStatisticsManager.cs index ce85ff20..b2abced6 100644 --- a/Ryujinx.HLE/HOS/Services/Sdb/Pdm/QueryService/QueryPlayStatisticsManager.cs +++ b/Ryujinx.HLE/HOS/Services/Sdb/Pdm/QueryService/QueryPlayStatisticsManager.cs @@ -1,5 +1,5 @@ -using ARMeilleure.Memory; -using Ryujinx.Common; +using Ryujinx.Common; +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Services.Account.Acc; using Ryujinx.HLE.HOS.Services.Sdb.Pdm.QueryService.Types; using System; @@ -37,7 +37,7 @@ namespace Ryujinx.HLE.HOS.Services.Sdb.Pdm.QueryService for (int i = 0; i < inputSize / sizeof(ulong); i++) { - titleIds.Add(BitConverter.ToUInt64(context.Memory.ReadBytes(inputPosition, inputSize), 0)); + titleIds.Add(context.Memory.Read((ulong)inputPosition)); } if (queryCapability == PlayLogQueryCapability.WhiteList) diff --git a/Ryujinx.HLE/HOS/Services/Sdb/Pl/ISharedFontManager.cs b/Ryujinx.HLE/HOS/Services/Sdb/Pl/ISharedFontManager.cs index 4560d954..001c38e2 100644 --- a/Ryujinx.HLE/HOS/Services/Sdb/Pl/ISharedFontManager.cs +++ b/Ryujinx.HLE/HOS/Services/Sdb/Pl/ISharedFontManager.cs @@ -116,11 +116,9 @@ namespace Ryujinx.HLE.HOS.Services.Sdb.Pl return false; } - context.Memory.WriteInt32(typesPosition + offset, (int)fontType); - - context.Memory.WriteInt32(offsetsPosition + offset, context.Device.System.Font.GetSharedMemoryAddressOffset(fontType)); - - context.Memory.WriteInt32(fontSizeBufferPosition + offset, context.Device.System.Font.GetFontSize(fontType)); + context.Memory.Write((ulong)(typesPosition + offset), (int)fontType); + context.Memory.Write((ulong)(offsetsPosition + offset), context.Device.System.Font.GetSharedMemoryAddressOffset(fontType)); + context.Memory.Write((ulong)(fontSizeBufferPosition + offset), context.Device.System.Font.GetFontSize(fontType)); return true; } diff --git a/Ryujinx.HLE/HOS/Services/Settings/ISettingsServer.cs b/Ryujinx.HLE/HOS/Services/Settings/ISettingsServer.cs index a8b10297..efd659f0 100644 --- a/Ryujinx.HLE/HOS/Services/Settings/ISettingsServer.cs +++ b/Ryujinx.HLE/HOS/Services/Settings/ISettingsServer.cs @@ -114,7 +114,7 @@ namespace Ryujinx.HLE.HOS.Services.Settings for (int index = 0; index < count; index++) { - context.Memory.WriteInt64(position, SystemStateMgr.GetLanguageCode(index)); + context.Memory.Write((ulong)position, SystemStateMgr.GetLanguageCode(index)); position += 8; } diff --git a/Ryujinx.HLE/HOS/Services/Settings/ISystemSettingsServer.cs b/Ryujinx.HLE/HOS/Services/Settings/ISystemSettingsServer.cs index 108cb56f..f3113e65 100644 --- a/Ryujinx.HLE/HOS/Services/Settings/ISystemSettingsServer.cs +++ b/Ryujinx.HLE/HOS/Services/Settings/ISystemSettingsServer.cs @@ -35,7 +35,7 @@ namespace Ryujinx.HLE.HOS.Services.Settings if (firmwareData != null) { - context.Memory.WriteBytes(replyPos, firmwareData); + context.Memory.Write((ulong)replyPos, firmwareData); return ResultCode.Success; } @@ -78,7 +78,7 @@ namespace Ryujinx.HLE.HOS.Services.Settings writer.Write(Encoding.ASCII.GetBytes(build)); - context.Memory.WriteBytes(replyPos, ms.ToArray()); + context.Memory.Write((ulong)replyPos, ms.ToArray()); } return ResultCode.Success; @@ -114,10 +114,15 @@ namespace Ryujinx.HLE.HOS.Services.Settings long namePos = context.Request.PtrBuff[1].Position; long nameSize = context.Request.PtrBuff[1].Size; - byte[] Class = context.Memory.ReadBytes(classPos, classSize); - byte[] name = context.Memory.ReadBytes(namePos, nameSize); + byte[] classBuffer = new byte[classSize]; - string askedSetting = Encoding.ASCII.GetString(Class).Trim('\0') + "!" + Encoding.ASCII.GetString(name).Trim('\0'); + context.Memory.Read((ulong)classPos, classBuffer); + + byte[] nameBuffer = new byte[nameSize]; + + context.Memory.Read((ulong)namePos, nameBuffer); + + string askedSetting = Encoding.ASCII.GetString(classBuffer).Trim('\0') + "!" + Encoding.ASCII.GetString(nameBuffer).Trim('\0'); NxSettings.Settings.TryGetValue(askedSetting, out object nxSetting); @@ -161,10 +166,15 @@ namespace Ryujinx.HLE.HOS.Services.Settings long replyPos = context.Request.ReceiveBuff[0].Position; long replySize = context.Request.ReceiveBuff[0].Size; - byte[] Class = context.Memory.ReadBytes(classPos, classSize); - byte[] name = context.Memory.ReadBytes(namePos, nameSize); + byte[] classBuffer = new byte[classSize]; - string askedSetting = Encoding.ASCII.GetString(Class).Trim('\0') + "!" + Encoding.ASCII.GetString(name).Trim('\0'); + context.Memory.Read((ulong)classPos, classBuffer); + + byte[] nameBuffer = new byte[nameSize]; + + context.Memory.Read((ulong)namePos, nameBuffer); + + string askedSetting = Encoding.ASCII.GetString(classBuffer).Trim('\0') + "!" + Encoding.ASCII.GetString(nameBuffer).Trim('\0'); NxSettings.Settings.TryGetValue(askedSetting, out object nxSetting); @@ -197,7 +207,7 @@ namespace Ryujinx.HLE.HOS.Services.Settings throw new NotImplementedException(nxSetting.GetType().Name); } - context.Memory.WriteBytes(replyPos, settingBuffer); + context.Memory.Write((ulong)replyPos, settingBuffer); Logger.PrintDebug(LogClass.ServiceSet, $"{askedSetting} set value: {nxSetting} as {nxSetting.GetType()}"); } diff --git a/Ryujinx.HLE/HOS/Services/Sockets/Bsd/IClient.cs b/Ryujinx.HLE/HOS/Services/Sockets/Bsd/IClient.cs index b2b3d052..f4dd8ef2 100644 --- a/Ryujinx.HLE/HOS/Services/Sockets/Bsd/IClient.cs +++ b/Ryujinx.HLE/HOS/Services/Sockets/Bsd/IClient.cs @@ -199,21 +199,23 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd private IPEndPoint ParseSockAddr(ServiceCtx context, long bufferPosition, long bufferSize) { - int size = context.Memory.ReadByte(bufferPosition); - int family = context.Memory.ReadByte(bufferPosition + 1); - int port = BinaryPrimitives.ReverseEndianness(context.Memory.ReadUInt16(bufferPosition + 2)); + int size = context.Memory.Read((ulong)bufferPosition); + int family = context.Memory.Read((ulong)bufferPosition + 1); + int port = BinaryPrimitives.ReverseEndianness(context.Memory.Read((ulong)bufferPosition + 2)); - byte[] rawIp = context.Memory.ReadBytes(bufferPosition + 4, 4); + byte[] rawIp = new byte[4]; + + context.Memory.Read((ulong)bufferPosition + 4, rawIp); return new IPEndPoint(new IPAddress(rawIp), port); } private void WriteSockAddr(ServiceCtx context, long bufferPosition, IPEndPoint endPoint) { - context.Memory.WriteByte(bufferPosition, 0); - context.Memory.WriteByte(bufferPosition + 1, (byte)endPoint.AddressFamily); - context.Memory.WriteUInt16(bufferPosition + 2, BinaryPrimitives.ReverseEndianness((ushort)endPoint.Port)); - context.Memory.WriteBytes(bufferPosition + 4, endPoint.Address.GetAddressBytes()); + context.Memory.Write((ulong)bufferPosition, (byte)0); + context.Memory.Write((ulong)bufferPosition + 1, (byte)endPoint.AddressFamily); + context.Memory.Write((ulong)bufferPosition + 2, BinaryPrimitives.ReverseEndianness((ushort)endPoint.Port)); + context.Memory.Write((ulong)bufferPosition + 4, endPoint.Address.GetAddressBytes()); } private void WriteSockAddr(ServiceCtx context, long bufferPosition, BsdSocket socket, bool isRemote) @@ -281,8 +283,11 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd int flags = context.RequestData.ReadInt32(); - byte[] rawPath = context.Memory.ReadBytes(bufferPosition, bufferSize); - string path = Encoding.ASCII.GetString(rawPath); + byte[] rawPath = new byte[bufferSize]; + + context.Memory.Read((ulong)bufferPosition, rawPath); + + string path = Encoding.ASCII.GetString(rawPath); WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP); @@ -321,7 +326,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd for (int i = 0; i < fdsCount; i++) { - int socketFd = context.Memory.ReadInt32(bufferPosition + i * 8); + int socketFd = context.Memory.Read((ulong)(bufferPosition + i * 8)); BsdSocket socket = RetrieveSocket(socketFd); @@ -329,8 +334,8 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd { return WriteBsdResult(context, -1, LinuxError.EBADF);} - PollEvent.EventTypeMask inputEvents = (PollEvent.EventTypeMask)context.Memory.ReadInt16(bufferPosition + i * 8 + 4); - PollEvent.EventTypeMask outputEvents = (PollEvent.EventTypeMask)context.Memory.ReadInt16(bufferPosition + i * 8 + 6); + PollEvent.EventTypeMask inputEvents = (PollEvent.EventTypeMask)context.Memory.Read((ulong)(bufferPosition + i * 8 + 4)); + PollEvent.EventTypeMask outputEvents = (PollEvent.EventTypeMask)context.Memory.Read((ulong)(bufferPosition + i * 8 + 6)); events[i] = new PollEvent(socketFd, socket, inputEvents, outputEvents); } @@ -405,8 +410,8 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd for (int i = 0; i < fdsCount; i++) { PollEvent Event = events[i]; - context.Memory.WriteInt32(bufferPosition + i * 8, Event.SocketFd); - context.Memory.WriteInt16(bufferPosition + i * 8 + 4, (short)Event.InputEvents); + context.Memory.Write((ulong)(bufferPosition + i * 8), Event.SocketFd); + context.Memory.Write((ulong)(bufferPosition + i * 8 + 4), (short)Event.InputEvents); PollEvent.EventTypeMask outputEvents = 0; @@ -435,7 +440,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd outputEvents |= PollEvent.EventTypeMask.Output; } - context.Memory.WriteInt16(bufferPosition + i * 8 + 6, (short)outputEvents); + context.Memory.Write((ulong)(bufferPosition + i * 8 + 6), (short)outputEvents); } return WriteBsdResult(context, readEvents.Count + writeEvents.Count + errorEvents.Count, LinuxError.SUCCESS); @@ -481,7 +486,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd result = socket.Handle.Receive(receivedBuffer, socketFlags); errno = SetResultErrno(socket.Handle, result); - context.Memory.WriteBytes(receivePosition, receivedBuffer); + context.Memory.Write((ulong)receivePosition, receivedBuffer); } catch (SocketException exception) { @@ -524,7 +529,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd result = socket.Handle.ReceiveFrom(receivedBuffer, receivedBuffer.Length, socketFlags, ref endPoint); errno = SetResultErrno(socket.Handle, result); - context.Memory.WriteBytes(receivePosition, receivedBuffer); + context.Memory.Write((ulong)receivePosition, receivedBuffer); WriteSockAddr(context, sockAddrOutPosition, (IPEndPoint)endPoint); } catch (SocketException exception) @@ -559,7 +564,9 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd return WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP); } - byte[] sendBuffer = context.Memory.ReadBytes(sendPosition, sendSize); + byte[] sendBuffer = new byte[sendSize]; + + context.Memory.Read((ulong)sendPosition, sendBuffer); try { @@ -600,8 +607,11 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd return WriteBsdResult(context, -1, LinuxError.EOPNOTSUPP); } - byte[] sendBuffer = context.Memory.ReadBytes(sendPosition, sendSize); - EndPoint endPoint = ParseSockAddr(context, bufferPosition, bufferSize); + byte[] sendBuffer = new byte[sendSize]; + + context.Memory.Read((ulong)sendPosition, sendBuffer); + + EndPoint endPoint = ParseSockAddr(context, bufferPosition, bufferSize); try { @@ -856,7 +866,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd (long bufferPosition, long bufferSize) = context.Request.GetBufferType0x22(); // FIXME: OOB not implemented. - context.Memory.WriteInt32(bufferPosition, 0); + context.Memory.Write((ulong)bufferPosition, 0); break; default: @@ -925,13 +935,13 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd case SocketOptionName.Type: case SocketOptionName.Linger: socket.Handle.GetSocketOption(SocketOptionLevel.Socket, optionName, optionValue); - context.Memory.WriteBytes(optionValuePosition, optionValue); + context.Memory.Write((ulong)optionValuePosition, optionValue); return LinuxError.SUCCESS; case (SocketOptionName)0x200: socket.Handle.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, optionValue); - context.Memory.WriteBytes(optionValuePosition, optionValue); + context.Memory.Write((ulong)optionValuePosition, optionValue); return LinuxError.SUCCESS; @@ -965,18 +975,18 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd case SocketOptionName.SendTimeout: case SocketOptionName.Type: case SocketOptionName.ReuseAddress: - socket.Handle.SetSocketOption(SocketOptionLevel.Socket, optionName, context.Memory.ReadInt32(optionValuePosition)); + socket.Handle.SetSocketOption(SocketOptionLevel.Socket, optionName, context.Memory.Read((ulong)optionValuePosition)); return LinuxError.SUCCESS; case (SocketOptionName)0x200: - socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, context.Memory.ReadInt32(optionValuePosition)); + socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, context.Memory.Read((ulong)optionValuePosition)); return LinuxError.SUCCESS; case SocketOptionName.Linger: socket.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, - new LingerOption(context.Memory.ReadInt32(optionValuePosition) != 0, context.Memory.ReadInt32(optionValuePosition + 4))); + new LingerOption(context.Memory.Read((ulong)optionValuePosition) != 0, context.Memory.Read((ulong)optionValuePosition + 4))); return LinuxError.SUCCESS; @@ -1100,7 +1110,9 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd if (socket != null) { - byte[] sendBuffer = context.Memory.ReadBytes(sendPosition, sendSize); + byte[] sendBuffer = new byte[sendSize]; + + context.Memory.Read((ulong)sendPosition, sendBuffer); try { diff --git a/Ryujinx.HLE/HOS/Services/Sockets/Nsd/IManager.cs b/Ryujinx.HLE/HOS/Services/Sockets/Nsd/IManager.cs index 277bc374..c5ceec5b 100644 --- a/Ryujinx.HLE/HOS/Services/Sockets/Nsd/IManager.cs +++ b/Ryujinx.HLE/HOS/Services/Sockets/Nsd/IManager.cs @@ -44,7 +44,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Nsd { byte[] settingNameBuffer = Encoding.UTF8.GetBytes(settingName + '\0'); - context.Memory.WriteBytes(outputPosition, settingNameBuffer); + context.Memory.Write((ulong)outputPosition, settingNameBuffer); } return result; @@ -62,7 +62,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Nsd { byte[] identifierBuffer = Encoding.UTF8.GetBytes(identifier + '\0'); - context.Memory.WriteBytes(outputPosition, identifierBuffer); + context.Memory.Write((ulong)outputPosition, identifierBuffer); } return result; @@ -138,7 +138,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Nsd byte[] resolvedAddressBuffer = Encoding.UTF8.GetBytes(resolvedAddress + '\0'); - context.Memory.WriteBytes(outputPosition, resolvedAddressBuffer); + context.Memory.Write((ulong)outputPosition, resolvedAddressBuffer); return result; } @@ -153,7 +153,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Nsd byte[] resolvedAddressBuffer = Encoding.UTF8.GetBytes(resolvedAddress + '\0'); - context.Memory.WriteBytes(outputPosition, resolvedAddressBuffer); + context.Memory.Write((ulong)outputPosition, resolvedAddressBuffer); context.ResponseData.Write((int)errorCode); diff --git a/Ryujinx.HLE/HOS/Services/Sockets/Nsd/Manager/FqdnResolver.cs b/Ryujinx.HLE/HOS/Services/Sockets/Nsd/Manager/FqdnResolver.cs index aaab7b3b..246c92bd 100644 --- a/Ryujinx.HLE/HOS/Services/Sockets/Nsd/Manager/FqdnResolver.cs +++ b/Ryujinx.HLE/HOS/Services/Sockets/Nsd/Manager/FqdnResolver.cs @@ -108,8 +108,11 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Nsd.Manager { (long inputPosition, long inputSize) = context.Request.GetBufferType0x21(); - byte[] addressBuffer = context.Memory.ReadBytes(inputPosition, inputSize); - string address = Encoding.UTF8.GetString(addressBuffer); + byte[] addressBuffer = new byte[inputSize]; + + context.Memory.Read((ulong)inputPosition, addressBuffer); + + string address = Encoding.UTF8.GetString(addressBuffer); resultCode = Resolve(context, address, out resolvedAddress); diff --git a/Ryujinx.HLE/HOS/Services/Sockets/Sfdnsres/IResolver.cs b/Ryujinx.HLE/HOS/Services/Sockets/Sfdnsres/IResolver.cs index 1cf2aa1c..a5e4c11b 100644 --- a/Ryujinx.HLE/HOS/Services/Sockets/Sfdnsres/IResolver.cs +++ b/Ryujinx.HLE/HOS/Services/Sockets/Sfdnsres/IResolver.cs @@ -21,37 +21,37 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres string hostName = hostEntry.HostName + '\0'; // h_name - context.Memory.WriteBytes(bufferPosition, Encoding.ASCII.GetBytes(hostName)); + context.Memory.Write((ulong)bufferPosition, Encoding.ASCII.GetBytes(hostName)); bufferPosition += hostName.Length; // h_aliases list size - context.Memory.WriteInt32(bufferPosition, IPAddress.HostToNetworkOrder(hostEntry.Aliases.Length)); + context.Memory.Write((ulong)bufferPosition, IPAddress.HostToNetworkOrder(hostEntry.Aliases.Length)); bufferPosition += 4; // Actual aliases foreach (string alias in hostEntry.Aliases) { - context.Memory.WriteBytes(bufferPosition, Encoding.ASCII.GetBytes(alias + '\0')); + context.Memory.Write((ulong)bufferPosition, Encoding.ASCII.GetBytes(alias + '\0')); bufferPosition += alias.Length + 1; } // h_addrtype but it's a short (also only support IPv4) - context.Memory.WriteInt16(bufferPosition, IPAddress.HostToNetworkOrder((short)2)); + context.Memory.Write((ulong)bufferPosition, IPAddress.HostToNetworkOrder((short)2)); bufferPosition += 2; // h_length but it's a short - context.Memory.WriteInt16(bufferPosition, IPAddress.HostToNetworkOrder((short)4)); + context.Memory.Write((ulong)bufferPosition, IPAddress.HostToNetworkOrder((short)4)); bufferPosition += 2; // Ip address count, we can only support ipv4 (blame Nintendo) - context.Memory.WriteInt32(bufferPosition, addresses != null ? IPAddress.HostToNetworkOrder(addresses.Count) : 0); + context.Memory.Write((ulong)bufferPosition, addresses != null ? IPAddress.HostToNetworkOrder(addresses.Count) : 0); bufferPosition += 4; if (addresses != null) { foreach (IPAddress ip in addresses) { - context.Memory.WriteInt32(bufferPosition, IPAddress.HostToNetworkOrder(BitConverter.ToInt32(ip.GetAddressBytes(), 0))); + context.Memory.Write((ulong)bufferPosition, IPAddress.HostToNetworkOrder(BitConverter.ToInt32(ip.GetAddressBytes(), 0))); bufferPosition += 4; } } @@ -168,8 +168,11 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres // GetHostByName(u8, u32, u64, pid, buffer) -> (u32, u32, u32, buffer) public ResultCode GetHostByName(ServiceCtx context) { - byte[] rawName = context.Memory.ReadBytes(context.Request.SendBuff[0].Position, context.Request.SendBuff[0].Size); - string name = Encoding.ASCII.GetString(rawName).TrimEnd('\0'); + byte[] rawName = new byte[context.Request.SendBuff[0].Size]; + + context.Memory.Read((ulong)context.Request.SendBuff[0].Position, rawName); + + string name = Encoding.ASCII.GetString(rawName).TrimEnd('\0'); // TODO: use params bool enableNsdResolve = context.RequestData.ReadInt32() == 1; @@ -248,7 +251,9 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres // GetHostByAddr(u32, u32, u32, u64, pid, buffer) -> (u32, u32, u32, buffer) public ResultCode GetHostByAddress(ServiceCtx context) { - byte[] rawIp = context.Memory.ReadBytes(context.Request.SendBuff[0].Position, context.Request.SendBuff[0].Size); + byte[] rawIp = new byte[context.Request.SendBuff[0].Size]; + + context.Memory.Read((ulong)context.Request.SendBuff[0].Position, rawIp); // TODO: use params uint socketLength = context.RequestData.ReadUInt32(); @@ -325,7 +330,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres if (errorString.Length + 1 <= context.Request.ReceiveBuff[0].Size) { resultCode = 0; - context.Memory.WriteBytes(context.Request.ReceiveBuff[0].Position, Encoding.ASCII.GetBytes(errorString + '\0')); + context.Memory.Write((ulong)context.Request.ReceiveBuff[0].Position, Encoding.ASCII.GetBytes(errorString + '\0')); } return resultCode; @@ -342,7 +347,7 @@ namespace Ryujinx.HLE.HOS.Services.Sockets.Sfdnsres if (errorString.Length + 1 <= context.Request.ReceiveBuff[0].Size) { resultCode = 0; - context.Memory.WriteBytes(context.Request.ReceiveBuff[0].Position, Encoding.ASCII.GetBytes(errorString + '\0')); + context.Memory.Write((ulong)context.Request.ReceiveBuff[0].Position, Encoding.ASCII.GetBytes(errorString + '\0')); } return resultCode; diff --git a/Ryujinx.HLE/HOS/Services/Spl/IRandomInterface.cs b/Ryujinx.HLE/HOS/Services/Spl/IRandomInterface.cs index 75c308af..a2b1943e 100644 --- a/Ryujinx.HLE/HOS/Services/Spl/IRandomInterface.cs +++ b/Ryujinx.HLE/HOS/Services/Spl/IRandomInterface.cs @@ -21,7 +21,7 @@ namespace Ryujinx.HLE.HOS.Services.Spl _rng.GetBytes(randomBytes); - context.Memory.WriteBytes(context.Request.ReceiveBuff[0].Position, randomBytes); + context.Memory.Write((ulong)context.Request.ReceiveBuff[0].Position, randomBytes); return ResultCode.Success; } diff --git a/Ryujinx.HLE/HOS/Services/SurfaceFlinger/IHOSBinderDriver.cs b/Ryujinx.HLE/HOS/Services/SurfaceFlinger/IHOSBinderDriver.cs index 42924d38..5546418f 100644 --- a/Ryujinx.HLE/HOS/Services/SurfaceFlinger/IHOSBinderDriver.cs +++ b/Ryujinx.HLE/HOS/Services/SurfaceFlinger/IHOSBinderDriver.cs @@ -24,7 +24,7 @@ namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger long replyPos = context.Request.ReceiveBuff[0].Position; long replySize = context.Request.ReceiveBuff[0].Size; - ReadOnlySpan inputParcel = context.Memory.GetSpan(dataPos, dataSize); + ReadOnlySpan inputParcel = context.Memory.GetSpan(dataPos, (int)dataSize); Span outputParcel = new Span(new byte[replySize]); @@ -32,7 +32,7 @@ namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger if (result == ResultCode.Success) { - context.Memory.WriteBytes(replyPos, outputParcel.ToArray()); + context.Memory.Write((ulong)replyPos, outputParcel); } return result; @@ -81,7 +81,7 @@ namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger (long dataPos, long dataSize) = context.Request.GetBufferType0x21(); (long replyPos, long replySize) = context.Request.GetBufferType0x22(); - ReadOnlySpan inputParcel = context.Memory.GetSpan((ulong)dataPos, (ulong)dataSize); + ReadOnlySpan inputParcel = context.Memory.GetSpan((ulong)dataPos, (int)dataSize); Span outputParcel = new Span(new byte[replySize]); @@ -89,7 +89,7 @@ namespace Ryujinx.HLE.HOS.Services.SurfaceFlinger if (result == ResultCode.Success) { - context.Memory.WriteBytes(replyPos, outputParcel.ToArray()); + context.Memory.Write((ulong)replyPos, outputParcel); } return result; diff --git a/Ryujinx.HLE/HOS/Services/Time/IStaticServiceForPsc.cs b/Ryujinx.HLE/HOS/Services/Time/IStaticServiceForPsc.cs index f5cecdbb..8de3f726 100644 --- a/Ryujinx.HLE/HOS/Services/Time/IStaticServiceForPsc.cs +++ b/Ryujinx.HLE/HOS/Services/Time/IStaticServiceForPsc.cs @@ -401,7 +401,11 @@ namespace Ryujinx.HLE.HOS.Services.Time { Debug.Assert(ipcDesc.Size == Marshal.SizeOf()); - using (BinaryReader bufferReader = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(ipcDesc.Position, ipcDesc.Size)))) + byte[] temp = new byte[ipcDesc.Size]; + + context.Memory.Read((ulong)ipcDesc.Position, temp); + + using (BinaryReader bufferReader = new BinaryReader(new MemoryStream(temp))) { return bufferReader.ReadStruct(); } @@ -418,7 +422,7 @@ namespace Ryujinx.HLE.HOS.Services.Time bufferWriter.WriteStruct(clockSnapshot); } - context.Memory.WriteBytes(ipcDesc.Position, memory.ToArray()); + context.Memory.Write((ulong)ipcDesc.Position, memory.ToArray()); memory.Dispose(); } } diff --git a/Ryujinx.HLE/HOS/Services/Time/ITimeServiceManager.cs b/Ryujinx.HLE/HOS/Services/Time/ITimeServiceManager.cs index a897b3f7..64b21381 100644 --- a/Ryujinx.HLE/HOS/Services/Time/ITimeServiceManager.cs +++ b/Ryujinx.HLE/HOS/Services/Time/ITimeServiceManager.cs @@ -123,7 +123,11 @@ namespace Ryujinx.HLE.HOS.Services.Time (long bufferPosition, long bufferSize) = context.Request.GetBufferType0x21(); - using (MemoryStream timeZoneBinaryStream = new MemoryStream(context.Memory.ReadBytes(bufferPosition, bufferSize))) + byte[] temp = new byte[bufferSize]; + + context.Memory.Read((ulong)bufferPosition, temp); + + using (MemoryStream timeZoneBinaryStream = new MemoryStream(temp)) { _timeManager.SetupTimeZoneManager(locationName, timeZoneUpdateTimePoint, totalLocationNameCount, timeZoneRuleVersion, timeZoneBinaryStream); } diff --git a/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForGlue.cs b/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForGlue.cs index 7acb0257..bbe80fbe 100644 --- a/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForGlue.cs +++ b/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForGlue.cs @@ -1,6 +1,5 @@ -using ARMeilleure.Memory; -using Ryujinx.Common; using Ryujinx.Common.Logging; +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Services.Time.TimeZone; using System; using System.Text; @@ -71,7 +70,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService return ResultCode.LocationNameTooLong; } - context.Memory.WriteBytes(bufferPosition + offset, Encoding.ASCII.GetBytes(locationName)); + context.Memory.Write((ulong)bufferPosition + offset, Encoding.ASCII.GetBytes(locationName)); MemoryHelper.FillWithZeros(context.Memory, bufferPosition + offset + locationName.Length, padding); offset += 0x24; diff --git a/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForPsc.cs b/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForPsc.cs index ed31fe7c..4f0e6e9c 100644 --- a/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForPsc.cs +++ b/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForPsc.cs @@ -1,6 +1,6 @@ -using ARMeilleure.Memory; -using Ryujinx.Common; +using Ryujinx.Common; using Ryujinx.Common.Logging; +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Services.Time.Clock; using Ryujinx.HLE.HOS.Services.Time.TimeZone; using Ryujinx.HLE.Utilities; @@ -129,7 +129,11 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService ResultCode result; - using (MemoryStream timeZoneBinaryStream = new MemoryStream(context.Memory.ReadBytes(bufferPosition, bufferSize))) + byte[] temp = new byte[bufferSize]; + + context.Memory.Read((ulong)bufferPosition, temp); + + using (MemoryStream timeZoneBinaryStream = new MemoryStream(temp)) { result = _timeZoneManager.SetDeviceLocationNameWithTimeZoneRule(locationName, timeZoneBinaryStream); } @@ -156,7 +160,11 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService ResultCode result; - using (MemoryStream timeZoneBinaryStream = new MemoryStream(context.Memory.ReadBytes(bufferPosition, bufferSize))) + byte[] temp = new byte[bufferSize]; + + context.Memory.Read((ulong)bufferPosition, temp); + + using (MemoryStream timeZoneBinaryStream = new MemoryStream(temp)) { result = _timeZoneManager.ParseTimeZoneRuleBinary(out TimeZoneRule timeZoneRule, timeZoneBinaryStream); @@ -246,7 +254,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService long outBufferPosition = context.Request.RecvListBuff[0].Position; long outBufferSize = context.Request.RecvListBuff[0].Size; - context.Memory.WriteInt64(outBufferPosition, posixTime); + context.Memory.Write((ulong)outBufferPosition, posixTime); context.ResponseData.Write(1); } @@ -266,7 +274,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService long outBufferPosition = context.Request.RecvListBuff[0].Position; long outBufferSize = context.Request.RecvListBuff[0].Size; - context.Memory.WriteInt64(outBufferPosition, posixTime); + context.Memory.Write((ulong)outBufferPosition, posixTime); // There could be only one result on one calendar as leap seconds aren't supported. context.ResponseData.Write(1); diff --git a/Ryujinx.HLE/HOS/Services/Time/TimeManager.cs b/Ryujinx.HLE/HOS/Services/Time/TimeManager.cs index 03ed6fba..7b17b18d 100644 --- a/Ryujinx.HLE/HOS/Services/Time/TimeManager.cs +++ b/Ryujinx.HLE/HOS/Services/Time/TimeManager.cs @@ -55,7 +55,7 @@ namespace Ryujinx.HLE.HOS.Services.Time EphemeralClockContextWriter = new EphemeralNetworkSystemClockContextWriter(); } - public void Initialize(Switch device, Horizon system, KSharedMemory sharedMemory, long timeSharedMemoryAddress, int timeSharedMemorySize) + public void Initialize(Switch device, Horizon system, KSharedMemory sharedMemory, ulong timeSharedMemoryAddress, int timeSharedMemorySize) { SharedMemory.Initialize(device, sharedMemory, timeSharedMemoryAddress, timeSharedMemorySize); diff --git a/Ryujinx.HLE/HOS/Services/Time/TimeSharedMemory.cs b/Ryujinx.HLE/HOS/Services/Time/TimeSharedMemory.cs index f714c662..e3683076 100644 --- a/Ryujinx.HLE/HOS/Services/Time/TimeSharedMemory.cs +++ b/Ryujinx.HLE/HOS/Services/Time/TimeSharedMemory.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; using Ryujinx.HLE.HOS.Kernel.Memory; @@ -13,7 +14,7 @@ namespace Ryujinx.HLE.HOS.Services.Time { private Switch _device; private KSharedMemory _sharedMemory; - private long _timeSharedMemoryAddress; + private ulong _timeSharedMemoryAddress; private int _timeSharedMemorySize; private const uint SteadyClockContextOffset = 0x00; @@ -21,7 +22,7 @@ namespace Ryujinx.HLE.HOS.Services.Time private const uint NetworkSystemClockContextOffset = 0x80; private const uint AutomaticCorrectionEnabledOffset = 0xC8; - public void Initialize(Switch device, KSharedMemory sharedMemory, long timeSharedMemoryAddress, int timeSharedMemorySize) + public void Initialize(Switch device, KSharedMemory sharedMemory, ulong timeSharedMemoryAddress, int timeSharedMemorySize) { _device = device; _sharedMemory = sharedMemory; @@ -29,7 +30,7 @@ namespace Ryujinx.HLE.HOS.Services.Time _timeSharedMemorySize = timeSharedMemorySize; // Clean the shared memory - _device.Memory.FillWithZeros(_timeSharedMemoryAddress, _timeSharedMemorySize); + _device.Memory.ZeroFill(_timeSharedMemoryAddress, (ulong)_timeSharedMemorySize); } public KSharedMemory GetSharedMemory() @@ -86,9 +87,9 @@ namespace Ryujinx.HLE.HOS.Services.Time WriteObjectToSharedMemory(NetworkSystemClockContextOffset, 4, context); } - private T ReadObjectFromSharedMemory(long offset, long padding) + private T ReadObjectFromSharedMemory(ulong offset, ulong padding) where T : unmanaged { - long indexOffset = _timeSharedMemoryAddress + offset; + ulong indexOffset = _timeSharedMemoryAddress + offset; T result; uint index; @@ -96,31 +97,31 @@ namespace Ryujinx.HLE.HOS.Services.Time do { - index = _device.Memory.ReadUInt32(indexOffset); + index = _device.Memory.Read(indexOffset); - long objectOffset = indexOffset + 4 + padding + (index & 1) * Marshal.SizeOf(); + ulong objectOffset = indexOffset + 4 + padding + (ulong)((index & 1) * Unsafe.SizeOf()); - result = _device.Memory.ReadStruct(objectOffset); + result = _device.Memory.Read(objectOffset); Thread.MemoryBarrier(); - possiblyNewIndex = _device.Memory.ReadUInt32(indexOffset); + possiblyNewIndex = _device.Memory.Read(indexOffset); } while (index != possiblyNewIndex); return result; } - private void WriteObjectToSharedMemory(long offset, long padding, T value) + private void WriteObjectToSharedMemory(ulong offset, ulong padding, T value) where T : unmanaged { - long indexOffset = _timeSharedMemoryAddress + offset; - uint newIndex = _device.Memory.ReadUInt32(indexOffset) + 1; - long objectOffset = indexOffset + 4 + padding + (newIndex & 1) * Marshal.SizeOf(); + ulong indexOffset = _timeSharedMemoryAddress + offset; + uint newIndex = _device.Memory.Read(indexOffset) + 1; + ulong objectOffset = indexOffset + 4 + padding + (ulong)((newIndex & 1) * Unsafe.SizeOf()); - _device.Memory.WriteStruct(objectOffset, value); + _device.Memory.Write(objectOffset, value); Thread.MemoryBarrier(); - _device.Memory.WriteUInt32(indexOffset, newIndex); + _device.Memory.Write(indexOffset, newIndex); } } } diff --git a/Ryujinx.HLE/HOS/Services/Vi/RootService/IApplicationDisplayService.cs b/Ryujinx.HLE/HOS/Services/Vi/RootService/IApplicationDisplayService.cs index b7869519..8180c284 100644 --- a/Ryujinx.HLE/HOS/Services/Vi/RootService/IApplicationDisplayService.cs +++ b/Ryujinx.HLE/HOS/Services/Vi/RootService/IApplicationDisplayService.cs @@ -1,4 +1,4 @@ -using ARMeilleure.Memory; +using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Ipc; using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Services.SurfaceFlinger; @@ -62,11 +62,11 @@ namespace Ryujinx.HLE.HOS.Services.Vi.RootService MemoryHelper.FillWithZeros(context.Memory, recBuffPtr, 0x60); // Add only the default display to buffer - context.Memory.WriteBytes(recBuffPtr, Encoding.ASCII.GetBytes("Default")); - context.Memory.WriteInt64(recBuffPtr + 0x40, 0x1L); - context.Memory.WriteInt64(recBuffPtr + 0x48, 0x1L); - context.Memory.WriteInt64(recBuffPtr + 0x50, 1280L); - context.Memory.WriteInt64(recBuffPtr + 0x58, 720L); + context.Memory.Write((ulong)recBuffPtr, Encoding.ASCII.GetBytes("Default")); + context.Memory.Write((ulong)recBuffPtr + 0x40, 0x1L); + context.Memory.Write((ulong)recBuffPtr + 0x48, 0x1L); + context.Memory.Write((ulong)recBuffPtr + 0x50, 1280L); + context.Memory.Write((ulong)recBuffPtr + 0x58, 720L); context.ResponseData.Write(1L); @@ -128,7 +128,7 @@ namespace Ryujinx.HLE.HOS.Services.Vi.RootService ReadOnlySpan parcelData = parcel.Finish(); - context.Memory.WriteBytes(parcelPtr, parcelData.ToArray()); + context.Memory.Write((ulong)parcelPtr, parcelData); context.ResponseData.Write((long)parcelData.Length); @@ -166,7 +166,7 @@ namespace Ryujinx.HLE.HOS.Services.Vi.RootService ReadOnlySpan parcelData = parcel.Finish(); - context.Memory.WriteBytes(parcelPtr, parcelData.ToArray()); + context.Memory.Write((ulong)parcelPtr, parcelData); context.ResponseData.Write(layerId); context.ResponseData.Write((long)parcelData.Length); diff --git a/Ryujinx.HLE/Loaders/Elf/ElfSymbol32.cs b/Ryujinx.HLE/Loaders/Elf/ElfSymbol32.cs index 32e8af7c..2b25d044 100644 --- a/Ryujinx.HLE/Loaders/Elf/ElfSymbol32.cs +++ b/Ryujinx.HLE/Loaders/Elf/ElfSymbol32.cs @@ -6,8 +6,8 @@ public uint NameOffset; public uint ValueAddress; public uint Size; - public char Info; - public char Other; + public byte Info; + public byte Other; public ushort SectionIndex; #pragma warning restore CS0649 } diff --git a/Ryujinx.HLE/Loaders/Elf/ElfSymbol64.cs b/Ryujinx.HLE/Loaders/Elf/ElfSymbol64.cs index 9290e014..acbd2191 100644 --- a/Ryujinx.HLE/Loaders/Elf/ElfSymbol64.cs +++ b/Ryujinx.HLE/Loaders/Elf/ElfSymbol64.cs @@ -4,8 +4,8 @@ { #pragma warning disable CS0649 public uint NameOffset; - public char Info; - public char Other; + public byte Info; + public byte Other; public ushort SectionIndex; public ulong ValueAddress; public ulong Size; diff --git a/Ryujinx.HLE/Ryujinx.HLE.csproj b/Ryujinx.HLE/Ryujinx.HLE.csproj index 77aaff65..168d9b13 100644 --- a/Ryujinx.HLE/Ryujinx.HLE.csproj +++ b/Ryujinx.HLE/Ryujinx.HLE.csproj @@ -44,7 +44,9 @@ + + diff --git a/Ryujinx.HLE/Switch.cs b/Ryujinx.HLE/Switch.cs index 6fcb3fb8..69da0b54 100644 --- a/Ryujinx.HLE/Switch.cs +++ b/Ryujinx.HLE/Switch.cs @@ -9,6 +9,7 @@ using Ryujinx.HLE.HOS; using Ryujinx.HLE.HOS.Services; using Ryujinx.HLE.HOS.Services.Hid; using Ryujinx.HLE.HOS.SystemState; +using Ryujinx.Memory; using System; using System.Threading; @@ -18,7 +19,7 @@ namespace Ryujinx.HLE { public IAalOutput AudioOut { get; private set; } - internal DeviceMemory Memory { get; private set; } + internal MemoryBlock Memory { get; private set; } public GpuContext Gpu { get; private set; } @@ -46,7 +47,7 @@ namespace Ryujinx.HLE AudioOut = audioOut; - Memory = new DeviceMemory(); + Memory = new MemoryBlock(1UL << 32); Gpu = new GpuContext(renderer); diff --git a/Ryujinx.HLE/Utilities/StringUtils.cs b/Ryujinx.HLE/Utilities/StringUtils.cs index f23a843f..4399da21 100644 --- a/Ryujinx.HLE/Utilities/StringUtils.cs +++ b/Ryujinx.HLE/Utilities/StringUtils.cs @@ -60,7 +60,7 @@ namespace Ryujinx.HLE.Utilities { while (size-- > 0) { - byte value = context.Memory.ReadByte(position++); + byte value = context.Memory.Read((ulong)position++); if (value == 0) { @@ -77,9 +77,9 @@ namespace Ryujinx.HLE.Utilities public static U8Span ReadUtf8Span(ServiceCtx context, int index = 0) { ulong position = (ulong)context.Request.PtrBuff[index].Position; - ulong size = (ulong)context.Request.PtrBuff[index].Size; + ulong size = (ulong)context.Request.PtrBuff[index].Size; - ReadOnlySpan buffer = context.Memory.GetSpan(position, size); + ReadOnlySpan buffer = context.Memory.GetSpan(position, (int)size); return new U8Span(buffer); } @@ -93,7 +93,7 @@ namespace Ryujinx.HLE.Utilities { while (size-- > 0) { - byte value = context.Memory.ReadByte(position++); + byte value = context.Memory.Read((ulong)position++); if (value == 0) { diff --git a/Ryujinx.HLE/Utilities/StructReader.cs b/Ryujinx.HLE/Utilities/StructReader.cs index cd274a95..0c45a8ee 100644 --- a/Ryujinx.HLE/Utilities/StructReader.cs +++ b/Ryujinx.HLE/Utilities/StructReader.cs @@ -1,4 +1,4 @@ -using ARMeilleure.Memory; +using Ryujinx.Cpu; using System.Runtime.InteropServices; namespace Ryujinx.HLE.Utilities diff --git a/Ryujinx.HLE/Utilities/StructWriter.cs b/Ryujinx.HLE/Utilities/StructWriter.cs index 1237746f..46a07a60 100644 --- a/Ryujinx.HLE/Utilities/StructWriter.cs +++ b/Ryujinx.HLE/Utilities/StructWriter.cs @@ -1,4 +1,4 @@ -using ARMeilleure.Memory; +using Ryujinx.Cpu; using System.Runtime.InteropServices; namespace Ryujinx.HLE.Utilities diff --git a/Ryujinx.Memory.Tests/Ryujinx.Memory.Tests.csproj b/Ryujinx.Memory.Tests/Ryujinx.Memory.Tests.csproj new file mode 100644 index 00000000..bc916ea2 --- /dev/null +++ b/Ryujinx.Memory.Tests/Ryujinx.Memory.Tests.csproj @@ -0,0 +1,19 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + + diff --git a/Ryujinx.Memory.Tests/Tests.cs b/Ryujinx.Memory.Tests/Tests.cs new file mode 100644 index 00000000..0f5fcaf5 --- /dev/null +++ b/Ryujinx.Memory.Tests/Tests.cs @@ -0,0 +1,42 @@ +using NUnit.Framework; +using Ryujinx.Memory; +using System; +using System.Runtime.InteropServices; + +namespace Ryujinx.Memory.Tests +{ + public class Tests + { + private const ulong MemorySize = 0x8000; + + private MemoryBlock _memoryBlock; + + [SetUp] + public void Setup() + { + _memoryBlock = new MemoryBlock(MemorySize); + } + + [TearDown] + public void Teardown() + { + _memoryBlock.Dispose(); + } + + [Test] + public void Test_Read() + { + Marshal.WriteInt32(_memoryBlock.Pointer, 0x2020, 0x1234abcd); + + Assert.AreEqual(_memoryBlock.Read(0x2020), 0x1234abcd); + } + + [Test] + public void Test_Write() + { + _memoryBlock.Write(0x2040, 0xbadc0de); + + Assert.AreEqual(Marshal.ReadInt32(_memoryBlock.Pointer, 0x2040), 0xbadc0de); + } + } +} \ No newline at end of file diff --git a/Ryujinx.Memory/MemoryAllocationFlags.cs b/Ryujinx.Memory/MemoryAllocationFlags.cs new file mode 100644 index 00000000..94025d38 --- /dev/null +++ b/Ryujinx.Memory/MemoryAllocationFlags.cs @@ -0,0 +1,28 @@ +using System; + +namespace Ryujinx.Memory +{ + /// + /// Flags that controls allocation and other properties of the memory block memory. + /// + [Flags] + public enum MemoryAllocationFlags + { + /// + /// No special allocation settings. + /// + None = 0, + + /// + /// Reserve a region of memory on the process address space, + /// without actually allocation any backing memory. + /// + Reserve = 1 << 0, + + /// + /// Enables read and write tracking of the memory block. + /// This currently does nothing and is reserved for future use. + /// + Tracked = 1 << 1 + } +} diff --git a/Ryujinx.Memory/MemoryBlock.cs b/Ryujinx.Memory/MemoryBlock.cs new file mode 100644 index 00000000..850fb115 --- /dev/null +++ b/Ryujinx.Memory/MemoryBlock.cs @@ -0,0 +1,276 @@ +using System; +using System.Runtime.CompilerServices; +using System.Threading; + +namespace Ryujinx.Memory +{ + /// + /// Represents a block of contiguous physical guest memory. + /// + public sealed class MemoryBlock : IDisposable + { + private IntPtr _pointer; + + /// + /// Pointer to the memory block data. + /// + public IntPtr Pointer => _pointer; + + /// + /// Size of the memory block. + /// + public ulong Size { get; } + + /// + /// Initializes a new instance of the memory block class. + /// + /// Size of the memory block + /// Flags that control memory block memory allocation + /// Throw when there's no enough memory to allocate the requested size + /// Throw when the current platform is not supported + public MemoryBlock(ulong size, MemoryAllocationFlags flags = MemoryAllocationFlags.None) + { + if (flags.HasFlag(MemoryAllocationFlags.Reserve)) + { + _pointer = MemoryManagement.Reserve(size); + } + else + { + _pointer = MemoryManagement.Allocate(size); + } + + Size = size; + } + + /// + /// Commits a region of memory that has previously been reserved. + /// This can be used to allocate memory on demand. + /// + /// Starting offset of the range to be committed + /// Size of the range to be committed + /// True if the operation was successful, false otherwise + /// Throw when the memory block has already been disposed + /// Throw when either or are out of range + public bool Commit(ulong offset, ulong size) + { + return MemoryManagement.Commit(GetPointerInternal(offset, size), size); + } + + /// + /// Reprotects a region of memory. + /// + /// Starting offset of the range to be reprotected + /// Size of the range to be reprotected + /// New memory permissions + /// Throw when the memory block has already been disposed + /// Throw when either or are out of range + /// Throw when is invalid + public void Reprotect(ulong offset, ulong size, MemoryPermission permission) + { + MemoryManagement.Reprotect(GetPointerInternal(offset, size), size, permission); + } + + /// + /// Reads bytes from the memory block. + /// + /// Starting offset of the range being read + /// Span where the bytes being read will be copied to + /// Throw when the memory block has already been disposed + /// Throw when the memory region specified for the the data is out of range + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Read(ulong offset, Span data) + { + GetSpan(offset, data.Length).CopyTo(data); + } + + /// + /// Reads data from the memory block. + /// + /// Type of the data + /// Offset where the data is located + /// Data at the specified address + /// Throw when the memory block has already been disposed + /// Throw when the memory region specified for the the data is out of range + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public T Read(ulong offset) where T : unmanaged + { + return GetRef(offset); + } + + /// + /// Writes bytes to the memory block. + /// + /// Starting offset of the range being written + /// Span where the bytes being written will be copied from + /// Throw when the memory block has already been disposed + /// Throw when the memory region specified for the the data is out of range + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Write(ulong offset, ReadOnlySpan data) + { + data.CopyTo(GetSpan(offset, data.Length)); + } + + /// + /// Writes data to the memory block. + /// + /// Type of the data being written + /// Offset to write the data into + /// Data to be written + /// Throw when the memory block has already been disposed + /// Throw when the memory region specified for the the data is out of range + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Write(ulong offset, T data) where T : unmanaged + { + GetRef(offset) = data; + } + + /// + /// Copies data from one memory location to another. + /// + /// Source offset to read the data from + /// Destination offset to write the data into + /// Size of the copy in bytes + /// Throw when the memory block has already been disposed + /// Throw when , or is out of range + public void Copy(ulong srcOffset, ulong dstOffset, ulong size) + { + const int MaxChunkSize = 1 << 30; + + for (ulong offset = 0; offset < size; offset += MaxChunkSize) + { + int copySize = (int)Math.Min(MaxChunkSize, size - offset); + + Write(dstOffset + offset, GetSpan(srcOffset + offset, copySize)); + } + } + + /// + /// Fills a region of memory with zeros. + /// + /// Offset of the region to fill with zeros + /// Size in bytes of the region to fill + /// Throw when the memory block has already been disposed + /// Throw when either or are out of range + public void ZeroFill(ulong offset, ulong size) + { + const int MaxChunkSize = 1 << 30; + + for (ulong subOffset = 0; subOffset < size; subOffset += MaxChunkSize) + { + int copySize = (int)Math.Min(MaxChunkSize, size - subOffset); + + GetSpan(offset + subOffset, copySize).Fill(0); + } + } + + /// + /// Gets a reference of the data at a given memory block region. + /// + /// Data type + /// Offset of the memory region + /// A reference to the given memory region data + /// Throw when the memory block has already been disposed + /// Throw when either or are out of range + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public unsafe ref T GetRef(ulong offset) where T : unmanaged + { + IntPtr ptr = _pointer; + + if (ptr == IntPtr.Zero) + { + ThrowObjectDisposed(); + } + + int size = Unsafe.SizeOf(); + + ulong endOffset = offset + (ulong)size; + + if (endOffset > Size || endOffset < offset) + { + ThrowArgumentOutOfRange(); + } + + return ref Unsafe.AsRef((void*)PtrAddr(ptr, offset)); + } + + /// + /// Gets the pointer of a given memory block region. + /// + /// Start offset of the memory region + /// Size in bytes of the region + /// The pointer to the memory region + /// Throw when the memory block has already been disposed + /// Throw when either or are out of range + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public IntPtr GetPointer(ulong offset, int size) => GetPointerInternal(offset, (ulong)size); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private IntPtr GetPointerInternal(ulong offset, ulong size) + { + IntPtr ptr = _pointer; + + if (ptr == IntPtr.Zero) + { + ThrowObjectDisposed(); + } + + ulong endOffset = offset + size; + + if (endOffset > Size || endOffset < offset) + { + ThrowArgumentOutOfRange(); + } + + return PtrAddr(ptr, offset); + } + + /// + /// Gets the span of a given memory block region. + /// + /// Start offset of the memory region + /// Size in bytes of the region + /// Span of the memory region + /// Throw when the memory block has already been disposed + /// Throw when either or are out of range + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public unsafe Span GetSpan(ulong offset, int size) + { + return new Span((void*)GetPointer(offset, size), size); + } + + /// + /// Adds a 64-bits offset to a native pointer. + /// + /// Native pointer + /// Offset to add + /// Native pointer with the added offset + private IntPtr PtrAddr(IntPtr pointer, ulong offset) + { + return (IntPtr)(pointer.ToInt64() + (long)offset); + } + + /// + /// Frees the memory allocated for this memory block. + /// + /// + /// It's an error to use the memory block after disposal. + /// + public void Dispose() => FreeMemory(); + + ~MemoryBlock() => FreeMemory(); + + private void FreeMemory() + { + IntPtr ptr = Interlocked.Exchange(ref _pointer, IntPtr.Zero); + + // If pointer is null, the memory was already freed or never allocated. + if (ptr != IntPtr.Zero) + { + MemoryManagement.Free(ptr); + } + } + + private void ThrowObjectDisposed() => throw new ObjectDisposedException(nameof(MemoryBlock)); + private void ThrowArgumentOutOfRange() => throw new ArgumentOutOfRangeException(); + } +} diff --git a/Ryujinx.Memory/MemoryConstants.cs b/Ryujinx.Memory/MemoryConstants.cs new file mode 100644 index 00000000..fc6f87e0 --- /dev/null +++ b/Ryujinx.Memory/MemoryConstants.cs @@ -0,0 +1,9 @@ +namespace Ryujinx.Memory +{ + static class MemoryConstants + { + public const int PageBits = 12; + public const int PageSize = 1 << PageBits; + public const int PageMask = PageSize - 1; + } +} diff --git a/ARMeilleure/Memory/MemoryManagement.cs b/Ryujinx.Memory/MemoryManagement.cs similarity index 81% rename from ARMeilleure/Memory/MemoryManagement.cs rename to Ryujinx.Memory/MemoryManagement.cs index ba62f8e7..2525bef7 100644 --- a/ARMeilleure/Memory/MemoryManagement.cs +++ b/Ryujinx.Memory/MemoryManagement.cs @@ -1,8 +1,7 @@ -using System; -using System.Runtime.CompilerServices; +using System; using System.Runtime.InteropServices; -namespace ARMeilleure.Memory +namespace Ryujinx.Memory { public static class MemoryManagement { @@ -25,18 +24,18 @@ namespace ARMeilleure.Memory } } - public static IntPtr AllocateWriteTracked(ulong size) + public static IntPtr Reserve(ulong size) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { IntPtr sizeNint = new IntPtr((long)size); - return MemoryManagementWindows.AllocateWriteTracked(sizeNint); + return MemoryManagementWindows.Reserve(sizeNint); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { - return MemoryManagementUnix.Allocate(size); + return MemoryManagementUnix.Reserve(size); } else { @@ -63,7 +62,7 @@ namespace ARMeilleure.Memory } } - public static void Reprotect(IntPtr address, ulong size, MemoryProtection permission) + public static void Reprotect(IntPtr address, ulong size, MemoryPermission permission) { bool result; @@ -89,25 +88,6 @@ namespace ARMeilleure.Memory } } - public static IntPtr Reserve(ulong size) - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - IntPtr sizeNint = new IntPtr((long)size); - - return MemoryManagementWindows.Reserve(sizeNint); - } - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || - RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - { - return MemoryManagementUnix.Reserve(size); - } - else - { - throw new PlatformNotSupportedException(); - } - } - public static bool Free(IntPtr address) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) diff --git a/Ryujinx.Memory/MemoryManagementUnix.cs b/Ryujinx.Memory/MemoryManagementUnix.cs new file mode 100644 index 00000000..81096867 --- /dev/null +++ b/Ryujinx.Memory/MemoryManagementUnix.cs @@ -0,0 +1,75 @@ +using Mono.Unix.Native; +using System; +using System.Collections.Concurrent; + +namespace Ryujinx.Memory +{ + static class MemoryManagementUnix + { + private static readonly ConcurrentDictionary _allocations = new ConcurrentDictionary(); + + public static IntPtr Allocate(ulong size) + { + return AllocateInternal(size, MmapProts.PROT_READ | MmapProts.PROT_WRITE); + } + + public static IntPtr Reserve(ulong size) + { + return AllocateInternal(size, MmapProts.PROT_NONE); + } + + private static IntPtr AllocateInternal(ulong size, MmapProts prot) + { + const MmapFlags flags = MmapFlags.MAP_PRIVATE | MmapFlags.MAP_ANONYMOUS; + + IntPtr ptr = Syscall.mmap(IntPtr.Zero, size, prot, flags, -1, 0); + + if (ptr == new IntPtr(-1L)) + { + throw new OutOfMemoryException(); + } + + if (!_allocations.TryAdd(ptr, size)) + { + // This should be impossible, kernel shouldn't return an already mapped address. + throw new InvalidOperationException(); + } + + return ptr; + } + + public static bool Commit(IntPtr address, ulong size) + { + return Syscall.mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) == 0; + } + + public static bool Reprotect(IntPtr address, ulong size, MemoryPermission permission) + { + return Syscall.mprotect(address, size, GetProtection(permission)) == 0; + } + + private static MmapProts GetProtection(MemoryPermission permission) + { + return permission switch + { + MemoryPermission.None => MmapProts.PROT_NONE, + MemoryPermission.Read => MmapProts.PROT_READ, + MemoryPermission.ReadAndWrite => MmapProts.PROT_READ | MmapProts.PROT_WRITE, + MemoryPermission.ReadAndExecute => MmapProts.PROT_READ | MmapProts.PROT_EXEC, + MemoryPermission.ReadWriteExecute => MmapProts.PROT_READ | MmapProts.PROT_WRITE | MmapProts.PROT_EXEC, + MemoryPermission.Execute => MmapProts.PROT_EXEC, + _ => throw new MemoryProtectionException(permission) + }; + } + + public static bool Free(IntPtr address) + { + if (_allocations.TryRemove(address, out ulong size)) + { + return Syscall.munmap(address, size) == 0; + } + + return false; + } + } +} \ No newline at end of file diff --git a/Ryujinx.Memory/MemoryManagementWindows.cs b/Ryujinx.Memory/MemoryManagementWindows.cs new file mode 100644 index 00000000..9513bb54 --- /dev/null +++ b/Ryujinx.Memory/MemoryManagementWindows.cs @@ -0,0 +1,106 @@ +using System; +using System.Runtime.InteropServices; + +namespace Ryujinx.Memory +{ + static class MemoryManagementWindows + { + [Flags] + private enum AllocationType : uint + { + Commit = 0x1000, + Reserve = 0x2000, + Decommit = 0x4000, + Release = 0x8000, + Reset = 0x80000, + Physical = 0x400000, + TopDown = 0x100000, + WriteWatch = 0x200000, + LargePages = 0x20000000 + } + + [Flags] + private enum MemoryProtection : uint + { + NoAccess = 0x01, + ReadOnly = 0x02, + ReadWrite = 0x04, + WriteCopy = 0x08, + Execute = 0x10, + ExecuteRead = 0x20, + ExecuteReadWrite = 0x40, + ExecuteWriteCopy = 0x80, + GuardModifierflag = 0x100, + NoCacheModifierflag = 0x200, + WriteCombineModifierflag = 0x400 + } + + [DllImport("kernel32.dll")] + private static extern IntPtr VirtualAlloc( + IntPtr lpAddress, + IntPtr dwSize, + AllocationType flAllocationType, + MemoryProtection flProtect); + + [DllImport("kernel32.dll")] + private static extern bool VirtualProtect( + IntPtr lpAddress, + IntPtr dwSize, + MemoryProtection flNewProtect, + out MemoryProtection lpflOldProtect); + + [DllImport("kernel32.dll")] + private static extern bool VirtualFree(IntPtr lpAddress, IntPtr dwSize, AllocationType dwFreeType); + + public static IntPtr Allocate(IntPtr size) + { + return AllocateInternal(size, AllocationType.Reserve | AllocationType.Commit); + } + + public static IntPtr Reserve(IntPtr size) + { + return AllocateInternal(size, AllocationType.Reserve); + } + + private static IntPtr AllocateInternal(IntPtr size, AllocationType flags = 0) + { + IntPtr ptr = VirtualAlloc(IntPtr.Zero, size, flags, MemoryProtection.ReadWrite); + + if (ptr == IntPtr.Zero) + { + throw new OutOfMemoryException(); + } + + return ptr; + } + + public static bool Commit(IntPtr location, IntPtr size) + { + return VirtualAlloc(location, size, AllocationType.Commit, MemoryProtection.ReadWrite) != IntPtr.Zero; + } + + public static bool Reprotect(IntPtr address, IntPtr size, MemoryPermission permission) + { + return VirtualProtect(address, size, GetProtection(permission), out _); + } + + private static MemoryProtection GetProtection(MemoryPermission permission) + { + return permission switch + { + MemoryPermission.None => MemoryProtection.NoAccess, + MemoryPermission.Read => MemoryProtection.ReadOnly, + MemoryPermission.ReadAndWrite => MemoryProtection.ReadWrite, + MemoryPermission.ReadAndExecute => MemoryProtection.ExecuteRead, + MemoryPermission.ReadWriteExecute => MemoryProtection.ExecuteReadWrite, + MemoryPermission.Execute => MemoryProtection.Execute, + _ => throw new MemoryProtectionException(permission) + }; + } + + public static bool Free(IntPtr address) + { + return VirtualFree(address, IntPtr.Zero, AllocationType.Release); + } + } +} \ No newline at end of file diff --git a/Ryujinx.Memory/MemoryPermission.cs b/Ryujinx.Memory/MemoryPermission.cs new file mode 100644 index 00000000..38f2d909 --- /dev/null +++ b/Ryujinx.Memory/MemoryPermission.cs @@ -0,0 +1,46 @@ +using System; + +namespace Ryujinx.Memory +{ + /// + /// Memory access permission control. + /// + [Flags] + public enum MemoryPermission + { + /// + /// No access is allowed on the memory region. + /// + None = 0, + + /// + /// Allow reads on the memory region. + /// + Read = 1 << 0, + + /// + /// Allow writes on the memory region. + /// + Write = 1 << 1, + + /// + /// Allow code execution on the memory region. + /// + Execute = 1 << 2, + + /// + /// Allow reads and writes on the memory region. + /// + ReadAndWrite = Read | Write, + + /// + /// Allow reads and code execution on the memory region. + /// + ReadAndExecute = Read | Execute, + + /// + /// Allow reads, writes, and code execution on the memory region. + /// + ReadWriteExecute = Read | Write | Execute + } +} diff --git a/Ryujinx.Memory/MemoryProtectionException.cs b/Ryujinx.Memory/MemoryProtectionException.cs new file mode 100644 index 00000000..27e950a1 --- /dev/null +++ b/Ryujinx.Memory/MemoryProtectionException.cs @@ -0,0 +1,23 @@ +using System; + +namespace Ryujinx.Memory +{ + class MemoryProtectionException : Exception + { + public MemoryProtectionException() + { + } + + public MemoryProtectionException(MemoryPermission permission) : base($"Failed to set memory protection to \"{permission}\".") + { + } + + public MemoryProtectionException(string message) : base(message) + { + } + + public MemoryProtectionException(string message, Exception innerException) : base(message, innerException) + { + } + } +} diff --git a/Ryujinx.Memory/Ryujinx.Memory.csproj b/Ryujinx.Memory/Ryujinx.Memory.csproj new file mode 100644 index 00000000..a5669b32 --- /dev/null +++ b/Ryujinx.Memory/Ryujinx.Memory.csproj @@ -0,0 +1,23 @@ + + + + netcoreapp3.1 + + + + true + + + + true + + + + + + + + + + + diff --git a/Ryujinx.Tests/Cpu/CpuTest.cs b/Ryujinx.Tests/Cpu/CpuTest.cs index fb9a8f17..b3f316f4 100644 --- a/Ryujinx.Tests/Cpu/CpuTest.cs +++ b/Ryujinx.Tests/Cpu/CpuTest.cs @@ -1,13 +1,12 @@ -using ARMeilleure.Memory; using ARMeilleure.State; -using ARMeilleure.Translation; using NUnit.Framework; - +using Ryujinx.Cpu; +using Ryujinx.Memory; using Ryujinx.Tests.Unicorn; using System; -using System.Runtime.InteropServices; +using MemoryPermission = Ryujinx.Tests.Unicorn.MemoryPermission; namespace Ryujinx.Tests.Cpu { @@ -15,17 +14,17 @@ namespace Ryujinx.Tests.Cpu public class CpuTest { private ulong _currAddress; - private long _size; + private ulong _size; private ulong _entryPoint; - private IntPtr _ramPointer; + private MemoryBlock _ram; private MemoryManager _memory; private ExecutionContext _context; - private Translator _translator; + private CpuContext _cpuContext; private static bool _unicornAvailable; private UnicornAArch64 _unicornEmu; @@ -48,18 +47,18 @@ namespace Ryujinx.Tests.Cpu _entryPoint = _currAddress; - _ramPointer = Marshal.AllocHGlobal(new IntPtr(_size)); - _memory = new MemoryManager(_ramPointer); - _memory.Map((long)_currAddress, 0, _size); + _ram = new MemoryBlock(_size); + _memory = new MemoryManager(_ram, 1UL << 16); + _memory.Map(_currAddress, 0, _size); - _context = new ExecutionContext(); + _context = CpuContext.CreateExecutionContext(); - _translator = new Translator(_memory); + _cpuContext = new CpuContext(_memory); if (_unicornAvailable) { _unicornEmu = new UnicornAArch64(); - _unicornEmu.MemoryMap(_currAddress, (ulong)_size, MemoryPermission.READ | MemoryPermission.EXEC); + _unicornEmu.MemoryMap(_currAddress, _size, MemoryPermission.READ | MemoryPermission.EXEC); _unicornEmu.PC = _entryPoint; } } @@ -67,10 +66,12 @@ namespace Ryujinx.Tests.Cpu [TearDown] public void Teardown() { - Marshal.FreeHGlobal(_ramPointer); + _memory.Dispose(); + _context.Dispose(); + _ram.Dispose(); _memory = null; _context = null; - _translator = null; + _cpuContext = null; _unicornEmu = null; } @@ -82,11 +83,11 @@ namespace Ryujinx.Tests.Cpu protected void Opcode(uint opcode) { - _memory.WriteUInt32((long)_currAddress, opcode); + _memory.Write(_currAddress, opcode); if (_unicornAvailable) { - _unicornEmu.MemoryWrite32((ulong)_currAddress, opcode); + _unicornEmu.MemoryWrite32(_currAddress, opcode); } _currAddress += 4; @@ -168,11 +169,11 @@ namespace Ryujinx.Tests.Cpu protected void ExecuteOpcodes() { - _translator.Execute(_context, _entryPoint); + _cpuContext.Execute(_context, _entryPoint); if (_unicornAvailable) { - _unicornEmu.RunForCount((ulong)(_currAddress - _entryPoint - 4) / 4); + _unicornEmu.RunForCount((_currAddress - _entryPoint - 4) / 4); } } diff --git a/Ryujinx.Tests/Cpu/CpuTest32.cs b/Ryujinx.Tests/Cpu/CpuTest32.cs index 0cd8f4df..9b8b89a1 100644 --- a/Ryujinx.Tests/Cpu/CpuTest32.cs +++ b/Ryujinx.Tests/Cpu/CpuTest32.cs @@ -1,10 +1,12 @@ -using ARMeilleure.Memory; -using ARMeilleure.State; -using ARMeilleure.Translation; +using ARMeilleure.State; + using NUnit.Framework; +using Ryujinx.Cpu; +using Ryujinx.Memory; using Ryujinx.Tests.Unicorn; + using System; -using System.Runtime.InteropServices; +using MemoryPermission = Ryujinx.Tests.Unicorn.MemoryPermission; namespace Ryujinx.Tests.Cpu { @@ -12,17 +14,17 @@ namespace Ryujinx.Tests.Cpu public class CpuTest32 { private uint _currAddress; - private long _size; + private ulong _size; private uint _entryPoint; - private IntPtr _ramPointer; + private MemoryBlock _ram; private MemoryManager _memory; private ExecutionContext _context; - private Translator _translator; + private CpuContext _cpuContext; private static bool _unicornAvailable; private UnicornAArch32 _unicornEmu; @@ -47,20 +49,20 @@ namespace Ryujinx.Tests.Cpu _entryPoint = _currAddress; - _ramPointer = Marshal.AllocHGlobal(new IntPtr(_size * 2)); - _memory = new MemoryManager(_ramPointer, addressSpaceBits: 16, useFlatPageTable: true); - _memory.Map((long)_currAddress, 0, _size*2); + _ram = new MemoryBlock(_size * 2); + _memory = new MemoryManager(_ram, 1UL << 16); + _memory.Map(_currAddress, 0, _size * 2); - _context = new ExecutionContext(); + _context = CpuContext.CreateExecutionContext(); _context.IsAarch32 = true; - _translator = new Translator(_memory); + _cpuContext = new CpuContext(_memory); if (_unicornAvailable) { _unicornEmu = new UnicornAArch32(); - _unicornEmu.MemoryMap(_currAddress, (ulong)_size, MemoryPermission.READ | MemoryPermission.EXEC); - _unicornEmu.MemoryMap((ulong)(_currAddress + _size), (ulong)_size, MemoryPermission.READ | MemoryPermission.WRITE); + _unicornEmu.MemoryMap(_currAddress, _size, MemoryPermission.READ | MemoryPermission.EXEC); + _unicornEmu.MemoryMap(_currAddress + _size, _size, MemoryPermission.READ | MemoryPermission.WRITE); _unicornEmu.PC = _entryPoint; } } @@ -68,10 +70,12 @@ namespace Ryujinx.Tests.Cpu [TearDown] public void Teardown() { - Marshal.FreeHGlobal(_ramPointer); + _memory.Dispose(); + _context.Dispose(); + _ram.Dispose(); _memory = null; _context = null; - _translator = null; + _cpuContext = null; _unicornEmu = null; } @@ -83,11 +87,11 @@ namespace Ryujinx.Tests.Cpu protected void Opcode(uint opcode) { - _memory.WriteUInt32((long)_currAddress, opcode); + _memory.Write(_currAddress, opcode); if (_unicornAvailable) { - _unicornEmu.MemoryWrite32((ulong)_currAddress, opcode); + _unicornEmu.MemoryWrite32(_currAddress, opcode); } _currAddress += 4; @@ -166,7 +170,7 @@ namespace Ryujinx.Tests.Cpu protected void ExecuteOpcodes(bool runUnicorn = true) { - _translator.Execute(_context, _entryPoint); + _cpuContext.Execute(_context, _entryPoint); if (_unicornAvailable && runUnicorn) { @@ -210,7 +214,7 @@ namespace Ryujinx.Tests.Cpu protected void SetWorkingMemory(byte[] data) { - _memory.WriteBytes(0x2000, data); + _memory.Write(0x2000, data); if (_unicornAvailable) { @@ -357,10 +361,10 @@ namespace Ryujinx.Tests.Cpu if (usingMemory) { - byte[] meilleureMem = _memory.ReadBytes((long)(0x2000), _size); - byte[] unicornMem = _unicornEmu.MemoryRead((ulong)(0x2000), (ulong)_size); + ReadOnlySpan meilleureMem = _memory.GetSpan(0x2000, (int)_size); + byte[] unicornMem = _unicornEmu.MemoryRead(0x2000, _size); - for (int i = 0; i < _size; i++) + for (int i = 0; i < (int)_size; i++) { Assert.AreEqual(meilleureMem[i], unicornMem[i]); } diff --git a/Ryujinx.Tests/Ryujinx.Tests.csproj b/Ryujinx.Tests/Ryujinx.Tests.csproj index ef7becff..92951daf 100644 --- a/Ryujinx.Tests/Ryujinx.Tests.csproj +++ b/Ryujinx.Tests/Ryujinx.Tests.csproj @@ -33,6 +33,8 @@ + + diff --git a/Ryujinx.sln b/Ryujinx.sln index 092e52a1..7635905c 100644 --- a/Ryujinx.sln +++ b/Ryujinx.sln @@ -40,6 +40,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution .editorconfig = .editorconfig EndProjectSection EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ryujinx.Memory", "Ryujinx.Memory\Ryujinx.Memory.csproj", "{A5E6C691-9E22-4263-8F40-42F002CE66BE}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ryujinx.Memory.Tests", "Ryujinx.Memory.Tests\Ryujinx.Memory.Tests.csproj", "{D1CC5322-7325-4F6B-9625-194B30BE1296}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ryujinx.Cpu", "Ryujinx.Cpu\Ryujinx.Cpu.csproj", "{3DF35E3D-D844-4399-A9A1-A9E923264C17}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -176,6 +182,30 @@ Global {79E4EE34-9C5F-4BE6-8529-A49D32B5B0CC}.Profile Release|Any CPU.Build.0 = Profile Release|Any CPU {79E4EE34-9C5F-4BE6-8529-A49D32B5B0CC}.Release|Any CPU.ActiveCfg = Release|Any CPU {79E4EE34-9C5F-4BE6-8529-A49D32B5B0CC}.Release|Any CPU.Build.0 = Release|Any CPU + {A5E6C691-9E22-4263-8F40-42F002CE66BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A5E6C691-9E22-4263-8F40-42F002CE66BE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A5E6C691-9E22-4263-8F40-42F002CE66BE}.Profile Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A5E6C691-9E22-4263-8F40-42F002CE66BE}.Profile Debug|Any CPU.Build.0 = Debug|Any CPU + {A5E6C691-9E22-4263-8F40-42F002CE66BE}.Profile Release|Any CPU.ActiveCfg = Release|Any CPU + {A5E6C691-9E22-4263-8F40-42F002CE66BE}.Profile Release|Any CPU.Build.0 = Release|Any CPU + {A5E6C691-9E22-4263-8F40-42F002CE66BE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A5E6C691-9E22-4263-8F40-42F002CE66BE}.Release|Any CPU.Build.0 = Release|Any CPU + {D1CC5322-7325-4F6B-9625-194B30BE1296}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D1CC5322-7325-4F6B-9625-194B30BE1296}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D1CC5322-7325-4F6B-9625-194B30BE1296}.Profile Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D1CC5322-7325-4F6B-9625-194B30BE1296}.Profile Debug|Any CPU.Build.0 = Debug|Any CPU + {D1CC5322-7325-4F6B-9625-194B30BE1296}.Profile Release|Any CPU.ActiveCfg = Release|Any CPU + {D1CC5322-7325-4F6B-9625-194B30BE1296}.Profile Release|Any CPU.Build.0 = Release|Any CPU + {D1CC5322-7325-4F6B-9625-194B30BE1296}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D1CC5322-7325-4F6B-9625-194B30BE1296}.Release|Any CPU.Build.0 = Release|Any CPU + {3DF35E3D-D844-4399-A9A1-A9E923264C17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3DF35E3D-D844-4399-A9A1-A9E923264C17}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3DF35E3D-D844-4399-A9A1-A9E923264C17}.Profile Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3DF35E3D-D844-4399-A9A1-A9E923264C17}.Profile Debug|Any CPU.Build.0 = Debug|Any CPU + {3DF35E3D-D844-4399-A9A1-A9E923264C17}.Profile Release|Any CPU.ActiveCfg = Release|Any CPU + {3DF35E3D-D844-4399-A9A1-A9E923264C17}.Profile Release|Any CPU.Build.0 = Release|Any CPU + {3DF35E3D-D844-4399-A9A1-A9E923264C17}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3DF35E3D-D844-4399-A9A1-A9E923264C17}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE