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