2019-01-18 17:26:39 -05:00
|
|
|
using System;
|
|
|
|
|
2018-12-18 00:33:36 -05:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
|
|
|
class KMemoryBlock
|
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
public ulong BaseAddress { get; private set; }
|
|
|
|
public ulong PagesCount { get; private set; }
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
public MemoryState State { get; private set; }
|
|
|
|
public MemoryPermission Permission { get; private set; }
|
|
|
|
public MemoryAttribute Attribute { get; private set; }
|
|
|
|
public MemoryPermission SourcePermission { get; private set; }
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
public int IpcRefCount { get; private set; }
|
|
|
|
public int DeviceRefCount { get; private set; }
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
public KMemoryBlock(
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong baseAddress,
|
|
|
|
ulong pagesCount,
|
|
|
|
MemoryState state,
|
|
|
|
MemoryPermission permission,
|
2019-01-18 17:26:39 -05:00
|
|
|
MemoryAttribute attribute,
|
|
|
|
int ipcRefCount = 0,
|
|
|
|
int deviceRefCount = 0)
|
|
|
|
{
|
|
|
|
BaseAddress = baseAddress;
|
|
|
|
PagesCount = pagesCount;
|
|
|
|
State = state;
|
|
|
|
Attribute = attribute;
|
|
|
|
Permission = permission;
|
|
|
|
IpcRefCount = ipcRefCount;
|
|
|
|
DeviceRefCount = deviceRefCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetState(MemoryPermission permission, MemoryState state, MemoryAttribute attribute)
|
|
|
|
{
|
|
|
|
Permission = permission;
|
|
|
|
State = state;
|
|
|
|
Attribute &= MemoryAttribute.IpcAndDeviceMapped;
|
|
|
|
Attribute |= attribute;
|
|
|
|
}
|
|
|
|
|
2020-07-17 00:19:07 -04:00
|
|
|
public void SetIpcMappingPermission(MemoryPermission newPermission)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
|
|
|
int oldIpcRefCount = IpcRefCount++;
|
|
|
|
|
|
|
|
if ((ushort)IpcRefCount == 0)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("IPC reference count increment overflowed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldIpcRefCount == 0)
|
|
|
|
{
|
2020-07-17 00:19:07 -04:00
|
|
|
SourcePermission = Permission;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
Permission &= ~MemoryPermission.ReadAndWrite;
|
2020-07-17 00:19:07 -04:00
|
|
|
Permission |= MemoryPermission.ReadAndWrite & newPermission;
|
2019-01-18 17:26:39 -05: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;
|
|
|
|
|
|
|
|
SourcePermission = MemoryPermission.None;
|
|
|
|
|
|
|
|
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 14:59:51 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
PagesCount += pagesCount;
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public KMemoryInfo GetInfo()
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong size = PagesCount * KMemoryManager.PageSize;
|
2018-08-15 14:59:51 -04:00
|
|
|
|
|
|
|
return new KMemoryInfo(
|
2018-11-28 17:18:09 -05:00
|
|
|
BaseAddress,
|
2018-12-06 06:16:24 -05:00
|
|
|
size,
|
2018-08-15 14:59:51 -04:00
|
|
|
State,
|
|
|
|
Permission,
|
|
|
|
Attribute,
|
2019-01-18 17:26:39 -05:00
|
|
|
SourcePermission,
|
2018-08-15 14:59:51 -04:00
|
|
|
IpcRefCount,
|
|
|
|
DeviceRefCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|