2019-01-18 23:26:39 +01:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
2020-07-19 20:24:18 +02:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
|
|
|
using System;
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2018-12-18 06:33:36 +01:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
class KTransferMemory : KAutoObject
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2020-07-19 20:24:18 +02:00
|
|
|
private KProcess _creator;
|
|
|
|
|
|
|
|
private readonly KPageList _pageList;
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
public ulong Address { get; private set; }
|
2020-07-19 20:24:18 +02:00
|
|
|
public ulong Size => _pageList.GetPagesCount() * KMemoryManager.PageSize;
|
|
|
|
|
|
|
|
public MemoryPermission Permission { get; private set; }
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2020-07-19 20:24:18 +02:00
|
|
|
private bool _hasBeenInitialized;
|
|
|
|
private bool _isMapped;
|
|
|
|
|
|
|
|
public KTransferMemory(KernelContext context) : base(context)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2020-07-19 20:24:18 +02:00
|
|
|
_pageList = new KPageList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult Initialize(ulong address, ulong size, MemoryPermission permission)
|
|
|
|
{
|
|
|
|
KProcess creator = KernelContext.Scheduler.GetCurrentProcess();
|
|
|
|
|
|
|
|
_creator = creator;
|
|
|
|
|
|
|
|
KernelResult result = creator.MemoryManager.BorrowTransferMemory(_pageList, address, size, permission);
|
|
|
|
|
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
creator.IncrementReferenceCount();
|
|
|
|
|
|
|
|
Permission = permission;
|
2018-12-06 12:16:24 +01:00
|
|
|
Address = address;
|
2020-07-19 20:24:18 +02:00
|
|
|
_hasBeenInitialized = true;
|
|
|
|
_isMapped = false;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Destroy()
|
|
|
|
{
|
|
|
|
if (_hasBeenInitialized)
|
|
|
|
{
|
|
|
|
if (!_isMapped && _creator.MemoryManager.UnborrowTransferMemory(Address, Size, _pageList) != KernelResult.Success)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Unexpected failure restoring transfer memory attributes.");
|
|
|
|
}
|
|
|
|
|
|
|
|
_creator.ResourceLimit?.Release(LimitableResource.TransferMemory, 1);
|
|
|
|
_creator.DecrementReferenceCount();
|
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|