2019-01-18 22:26:39 +00:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
2020-07-19 18:24:18 +00:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
|
|
|
using System;
|
2019-01-18 22:26:39 +00:00
|
|
|
|
2018-12-18 05:33:36 +00:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2019-01-18 22:26:39 +00:00
|
|
|
class KTransferMemory : KAutoObject
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2020-07-19 18:24:18 +00:00
|
|
|
private KProcess _creator;
|
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
// TODO: Remove when we no longer need to read it from the owner directly.
|
|
|
|
public KProcess Creator => _creator;
|
|
|
|
|
2020-07-19 18:24:18 +00:00
|
|
|
private readonly KPageList _pageList;
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public ulong Address { get; private set; }
|
2020-07-19 18:24:18 +00:00
|
|
|
public ulong Size => _pageList.GetPagesCount() * KMemoryManager.PageSize;
|
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
public KMemoryPermission Permission { get; private set; }
|
2018-08-15 18:59:51 +00:00
|
|
|
|
2020-07-19 18:24:18 +00:00
|
|
|
private bool _hasBeenInitialized;
|
|
|
|
private bool _isMapped;
|
|
|
|
|
|
|
|
public KTransferMemory(KernelContext context) : base(context)
|
2018-08-15 18:59:51 +00:00
|
|
|
{
|
2020-07-19 18:24:18 +00:00
|
|
|
_pageList = new KPageList();
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:23:43 +00:00
|
|
|
public KernelResult Initialize(ulong address, ulong size, KMemoryPermission permission)
|
2020-07-19 18:24:18 +00:00
|
|
|
{
|
2020-12-09 22:20:05 +00:00
|
|
|
KProcess creator = KernelStatic.GetCurrentProcess();
|
2020-07-19 18:24:18 +00:00
|
|
|
|
|
|
|
_creator = creator;
|
|
|
|
|
|
|
|
KernelResult result = creator.MemoryManager.BorrowTransferMemory(_pageList, address, size, permission);
|
|
|
|
|
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
creator.IncrementReferenceCount();
|
|
|
|
|
|
|
|
Permission = permission;
|
2018-12-06 11:16:24 +00:00
|
|
|
Address = address;
|
2020-07-19 18:24:18 +00: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 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|