2019-01-18 14:26:39 -08:00
|
|
|
using System;
|
|
|
|
|
2018-12-17 21:33:36 -08:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
2018-08-15 11:59:51 -07:00
|
|
|
{
|
|
|
|
class KMemoryBlock
|
|
|
|
{
|
2019-01-18 14:26:39 -08:00
|
|
|
public ulong BaseAddress { get; private set; }
|
|
|
|
public ulong PagesCount { get; private set; }
|
2018-08-15 11:59:51 -07:00
|
|
|
|
2019-01-18 14:26:39 -08:00
|
|
|
public MemoryState State { get; private set; }
|
2020-12-01 15:23:43 -08:00
|
|
|
public KMemoryPermission Permission { get; private set; }
|
2019-01-18 14:26:39 -08:00
|
|
|
public MemoryAttribute Attribute { get; private set; }
|
2020-12-01 15:23:43 -08:00
|
|
|
public KMemoryPermission SourcePermission { get; private set; }
|
2018-08-15 11:59:51 -07:00
|
|
|
|
2019-01-18 14:26:39 -08:00
|
|
|
public int IpcRefCount { get; private set; }
|
|
|
|
public int DeviceRefCount { get; private set; }
|
2018-08-15 11:59:51 -07:00
|
|
|
|
|
|
|
public KMemoryBlock(
|
2018-12-06 03:16:24 -08:00
|
|
|
ulong baseAddress,
|
|
|
|
ulong pagesCount,
|
|
|
|
MemoryState state,
|
2020-12-01 15:23:43 -08:00
|
|
|
KMemoryPermission permission,
|
2019-01-18 14:26:39 -08:00
|
|
|
MemoryAttribute attribute,
|
|
|
|
int ipcRefCount = 0,
|
|
|
|
int deviceRefCount = 0)
|
|
|
|
{
|
|
|
|
BaseAddress = baseAddress;
|
|
|
|
PagesCount = pagesCount;
|
|
|
|
State = state;
|
|
|
|
Attribute = attribute;
|
|
|
|
Permission = permission;
|
|
|
|
IpcRefCount = ipcRefCount;
|
|
|
|
DeviceRefCount = deviceRefCount;
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:23:43 -08:00
|
|
|
public void SetState(KMemoryPermission permission, MemoryState state, MemoryAttribute attribute)
|
2019-01-18 14:26:39 -08:00
|
|
|
{
|
|
|
|
Permission = permission;
|
|
|
|
State = state;
|
|
|
|
Attribute &= MemoryAttribute.IpcAndDeviceMapped;
|
|
|
|
Attribute |= attribute;
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:23:43 -08:00
|
|
|
public void SetIpcMappingPermission(KMemoryPermission newPermission)
|
2019-01-18 14:26:39 -08:00
|
|
|
{
|
|
|
|
int oldIpcRefCount = IpcRefCount++;
|
|
|
|
|
|
|
|
if ((ushort)IpcRefCount == 0)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("IPC reference count increment overflowed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldIpcRefCount == 0)
|
|
|
|
{
|
2020-07-16 21:19:07 -07:00
|
|
|
SourcePermission = Permission;
|
2019-01-18 14:26:39 -08:00
|
|
|
|
2020-12-01 15:23:43 -08:00
|
|
|
Permission &= ~KMemoryPermission.ReadAndWrite;
|
|
|
|
Permission |= KMemoryPermission.ReadAndWrite & newPermission;
|
2019-01-18 14:26:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Attribute |= MemoryAttribute.IpcMapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RestoreIpcMappingPermission()
|
|
|
|
{
|
|
|
|
int oldIpcRefCount = IpcRefCount--;
|
|
|
|
|
|
|
|
if (oldIpcRefCount == 0)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("IPC reference count decrement underflowed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldIpcRefCount == 1)
|
|
|
|
{
|
|
|
|
Permission = SourcePermission;
|
|
|
|
|
2020-12-01 15:23:43 -08:00
|
|
|
SourcePermission = KMemoryPermission.None;
|
2019-01-18 14:26:39 -08:00
|
|
|
|
|
|
|
Attribute &= ~MemoryAttribute.IpcMapped;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public KMemoryBlock SplitRightAtAddress(ulong address)
|
|
|
|
{
|
|
|
|
ulong leftAddress = BaseAddress;
|
|
|
|
|
|
|
|
ulong leftPagesCount = (address - leftAddress) / KMemoryManager.PageSize;
|
|
|
|
|
|
|
|
BaseAddress = address;
|
|
|
|
|
|
|
|
PagesCount -= leftPagesCount;
|
|
|
|
|
|
|
|
return new KMemoryBlock(
|
|
|
|
leftAddress,
|
|
|
|
leftPagesCount,
|
|
|
|
State,
|
|
|
|
Permission,
|
|
|
|
Attribute,
|
|
|
|
IpcRefCount,
|
|
|
|
DeviceRefCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AddPages(ulong pagesCount)
|
2018-08-15 11:59:51 -07:00
|
|
|
{
|
2019-01-18 14:26:39 -08:00
|
|
|
PagesCount += pagesCount;
|
2018-08-15 11:59:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public KMemoryInfo GetInfo()
|
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
ulong size = PagesCount * KMemoryManager.PageSize;
|
2018-08-15 11:59:51 -07:00
|
|
|
|
|
|
|
return new KMemoryInfo(
|
2018-11-28 14:18:09 -08:00
|
|
|
BaseAddress,
|
2018-12-06 03:16:24 -08:00
|
|
|
size,
|
2018-08-15 11:59:51 -07:00
|
|
|
State,
|
|
|
|
Permission,
|
|
|
|
Attribute,
|
2019-01-18 14:26:39 -08:00
|
|
|
SourcePermission,
|
2018-08-15 11:59:51 -07:00
|
|
|
IpcRefCount,
|
|
|
|
DeviceRefCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|