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