0
0
Fork 0
mirror of https://github.com/GreemDev/Ryujinx.git synced 2024-12-23 06:55:48 +00:00

kernel: Implement MapTransferMemory and UnmapTransferMemory (#2386)

Based on my reversing of kernel 12.0.0
This commit is contained in:
Mary 2021-06-23 21:52:11 +02:00 committed by GitHub
parent 50ba233ac6
commit 0644db02ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 199 additions and 0 deletions

View file

@ -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<HostMemoryRange> _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<HostMemoryRange>();
}
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)

View file

@ -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<KTransferMemory>(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<KTransferMemory>(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))

View file

@ -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);

View file

@ -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);

View file

@ -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) },