From 0644db02ad5d800fee4a3f7caad3cd0c8f5fbcdc Mon Sep 17 00:00:00 2001 From: Mary Date: Wed, 23 Jun 2021 21:52:11 +0200 Subject: [PATCH] kernel: Implement MapTransferMemory and UnmapTransferMemory (#2386) Based on my reversing of kernel 12.0.0 --- .../HOS/Kernel/Memory/KTransferMemory.cs | 89 +++++++++++++++++++ .../HOS/Kernel/SupervisorCall/Syscall.cs | 86 ++++++++++++++++++ .../HOS/Kernel/SupervisorCall/Syscall32.cs | 10 +++ .../HOS/Kernel/SupervisorCall/Syscall64.cs | 10 +++ .../HOS/Kernel/SupervisorCall/SyscallTable.cs | 4 + 5 files changed, 199 insertions(+) diff --git a/Ryujinx.HLE/HOS/Kernel/Memory/KTransferMemory.cs b/Ryujinx.HLE/HOS/Kernel/Memory/KTransferMemory.cs index c75d8e6981..3051d998ca 100644 --- a/Ryujinx.HLE/HOS/Kernel/Memory/KTransferMemory.cs +++ b/Ryujinx.HLE/HOS/Kernel/Memory/KTransferMemory.cs @@ -1,3 +1,4 @@ +using Ryujinx.Common; using Ryujinx.HLE.HOS.Kernel.Common; using Ryujinx.HLE.HOS.Kernel.Process; using Ryujinx.Memory.Range; @@ -15,6 +16,8 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory private readonly List _ranges; + private readonly SharedMemoryStorage _storage; + public ulong Address { get; private set; } public ulong Size { get; private set; } @@ -28,6 +31,15 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory _ranges = new List(); } + public KTransferMemory(KernelContext context, SharedMemoryStorage storage) : base(context) + { + _storage = storage; + Permission = KMemoryPermission.ReadAndWrite; + + _hasBeenInitialized = true; + _isMapped = false; + } + public KernelResult Initialize(ulong address, ulong size, KMemoryPermission permission) { KProcess creator = KernelStatic.GetCurrentProcess(); @@ -52,6 +64,83 @@ namespace Ryujinx.HLE.HOS.Kernel.Memory return result; } + public KernelResult MapIntoProcess( + KPageTableBase memoryManager, + ulong address, + ulong size, + KProcess process, + KMemoryPermission permission) + { + if (_storage == null) + { + throw new NotImplementedException(); + } + + ulong pagesCountRounded = BitUtils.DivRoundUp(size, KPageTableBase.PageSize); + + var pageList = _storage.GetPageList(); + if (pageList.GetPagesCount() != pagesCountRounded) + { + return KernelResult.InvalidSize; + } + + if (permission != Permission || _isMapped) + { + return KernelResult.InvalidState; + } + + MemoryState state = Permission == KMemoryPermission.None ? MemoryState.TransferMemoryIsolated : MemoryState.TransferMemory; + + KernelResult result = memoryManager.MapPages(address, pageList, state, KMemoryPermission.ReadAndWrite); + + if (result == KernelResult.Success) + { + _isMapped = true; + + if (!memoryManager.SupportsMemoryAliasing) + { + _storage.Borrow(process, address); + } + } + + return result; + } + + public KernelResult UnmapFromProcess( + KPageTableBase memoryManager, + ulong address, + ulong size, + KProcess process) + { + if (_storage == null) + { + throw new NotImplementedException(); + } + + ulong pagesCountRounded = BitUtils.DivRoundUp(size, KPageTableBase.PageSize); + + var pageList = _storage.GetPageList(); + ulong pagesCount = pageList.GetPagesCount(); + + if (pagesCount != pagesCountRounded) + { + return KernelResult.InvalidSize; + } + + var ranges = _storage.GetRanges(); + + MemoryState state = Permission == KMemoryPermission.None ? MemoryState.TransferMemoryIsolated : MemoryState.TransferMemory; + + KernelResult result = memoryManager.UnmapPages(address, pagesCount, ranges, state); + + if (result == KernelResult.Success) + { + _isMapped = false; + } + + return result; + } + protected override void Destroy() { if (_hasBeenInitialized) diff --git a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/Syscall.cs b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/Syscall.cs index b3e202b30f..d65a373dd1 100644 --- a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/Syscall.cs +++ b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/Syscall.cs @@ -1095,6 +1095,92 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall return result; } + public KernelResult MapTransferMemory(int handle, ulong address, ulong size, KMemoryPermission permission) + { + if (!PageAligned(address)) + { + return KernelResult.InvalidAddress; + } + + if (!PageAligned(size) || size == 0) + { + return KernelResult.InvalidSize; + } + + if (address + size <= address) + { + return KernelResult.InvalidMemState; + } + + if (permission > KMemoryPermission.ReadAndWrite || permission == KMemoryPermission.Write) + { + return KernelResult.InvalidPermission; + } + + KProcess currentProcess = KernelStatic.GetCurrentProcess(); + + KTransferMemory transferMemory = currentProcess.HandleTable.GetObject(handle); + + if (transferMemory == null) + { + return KernelResult.InvalidHandle; + } + + if (currentProcess.MemoryManager.IsInvalidRegion(address, size) || + currentProcess.MemoryManager.InsideHeapRegion(address, size) || + currentProcess.MemoryManager.InsideAliasRegion(address, size)) + { + return KernelResult.InvalidMemRange; + } + + return transferMemory.MapIntoProcess( + currentProcess.MemoryManager, + address, + size, + currentProcess, + permission); + } + + public KernelResult UnmapTransferMemory(int handle, ulong address, ulong size) + { + if (!PageAligned(address)) + { + return KernelResult.InvalidAddress; + } + + if (!PageAligned(size) || size == 0) + { + return KernelResult.InvalidSize; + } + + if (address + size <= address) + { + return KernelResult.InvalidMemState; + } + + KProcess currentProcess = KernelStatic.GetCurrentProcess(); + + KTransferMemory transferMemory = currentProcess.HandleTable.GetObject(handle); + + if (transferMemory == null) + { + return KernelResult.InvalidHandle; + } + + if (currentProcess.MemoryManager.IsInvalidRegion(address, size) || + currentProcess.MemoryManager.InsideHeapRegion(address, size) || + currentProcess.MemoryManager.InsideAliasRegion(address, size)) + { + return KernelResult.InvalidMemRange; + } + + return transferMemory.UnmapFromProcess( + currentProcess.MemoryManager, + address, + size, + currentProcess); + } + public KernelResult MapPhysicalMemory(ulong address, ulong size) { if (!PageAligned(address)) diff --git a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/Syscall32.cs b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/Syscall32.cs index 97915d14c9..1875facbc9 100644 --- a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/Syscall32.cs +++ b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/Syscall32.cs @@ -135,6 +135,16 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall return _syscall.CreateTransferMemory(address, size, permission, out handle); } + public KernelResult MapTransferMemory32([R(0)] int handle, [R(1)] uint address, [R(2)] uint size, [R(3)] KMemoryPermission permission) + { + return _syscall.MapTransferMemory(handle, address, size, permission); + } + + public KernelResult UnmapTransferMemory32([R(0)] int handle, [R(1)] uint address, [R(2)] uint size) + { + return _syscall.UnmapTransferMemory(handle, address, size); + } + public KernelResult MapPhysicalMemory32([R(0)] uint address, [R(1)] uint size) { return _syscall.MapPhysicalMemory(address, size); diff --git a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/Syscall64.cs b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/Syscall64.cs index 07ca4aae7b..c22397cf6a 100644 --- a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/Syscall64.cs +++ b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/Syscall64.cs @@ -152,6 +152,16 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall return _syscall.CreateTransferMemory(address, size, permission, out handle); } + public KernelResult MapTransferMemory64([R(0)] int handle, [R(1)] ulong address, [R(2)] ulong size, [R(3)] KMemoryPermission permission) + { + return _syscall.MapTransferMemory(handle, address, size, permission); + } + + public KernelResult UnmapTransferMemory64([R(0)] int handle, [R(1)] ulong address, [R(2)] ulong size) + { + return _syscall.UnmapTransferMemory(handle, address, size); + } + public KernelResult MapPhysicalMemory64([R(0)] ulong address, [R(1)] ulong size) { return _syscall.MapPhysicalMemory(address, size); diff --git a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SyscallTable.cs b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SyscallTable.cs index 02b6fb3cb0..790a3179d2 100644 --- a/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SyscallTable.cs +++ b/Ryujinx.HLE/HOS/Kernel/SupervisorCall/SyscallTable.cs @@ -73,6 +73,8 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall { 0x43, nameof(Syscall64.ReplyAndReceive64) }, { 0x44, nameof(Syscall64.ReplyAndReceiveWithUserBuffer64) }, { 0x45, nameof(Syscall64.CreateEvent64) }, + { 0x51, nameof(Syscall64.MapTransferMemory64) }, + { 0x52, nameof(Syscall64.UnmapTransferMemory64) }, { 0x65, nameof(Syscall64.GetProcessList64) }, { 0x6f, nameof(Syscall64.GetSystemInfo64) }, { 0x70, nameof(Syscall64.CreatePort64) }, @@ -138,6 +140,8 @@ namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall { 0x41, nameof(Syscall32.AcceptSession32) }, { 0x43, nameof(Syscall32.ReplyAndReceive32) }, { 0x45, nameof(Syscall32.CreateEvent32) }, + { 0x51, nameof(Syscall32.MapTransferMemory32) }, + { 0x52, nameof(Syscall32.UnmapTransferMemory32) }, { 0x5F, nameof(Syscall32.FlushProcessDataCache32) }, { 0x65, nameof(Syscall32.GetProcessList32) }, { 0x6f, nameof(Syscall32.GetSystemInfo32) },