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; }
|
|
|
|
public MemoryPermission Permission { get; private set; }
|
|
|
|
public MemoryAttribute Attribute { get; private set; }
|
|
|
|
public MemoryPermission 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,
|
|
|
|
MemoryPermission 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetState(MemoryPermission permission, MemoryState state, MemoryAttribute attribute)
|
|
|
|
{
|
|
|
|
Permission = permission;
|
|
|
|
State = state;
|
|
|
|
Attribute &= MemoryAttribute.IpcAndDeviceMapped;
|
|
|
|
Attribute |= attribute;
|
|
|
|
}
|
|
|
|
|
2020-07-16 21:19:07 -07:00
|
|
|
public void SetIpcMappingPermission(MemoryPermission 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
|
|
|
|
|
|
|
Permission &= ~MemoryPermission.ReadAndWrite;
|
2020-07-16 21:19:07 -07:00
|
|
|
Permission |= MemoryPermission.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;
|
|
|
|
|
|
|
|
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 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|