Ryujinx/Ryujinx.HLE/HOS/Kernel/KSharedMemory.cs

68 lines
2 KiB
C#
Raw Normal View History

using Ryujinx.Common;
namespace Ryujinx.HLE.HOS.Kernel
{
2018-12-01 21:38:15 +01:00
internal class KSharedMemory
{
2018-12-01 21:01:59 +01:00
private KPageList _pageList;
2018-12-01 21:01:59 +01:00
private long _ownerPid;
2018-12-01 21:01:59 +01:00
private MemoryPermission _ownerPermission;
private MemoryPermission _userPermission;
public KSharedMemory(
2018-12-01 21:01:59 +01:00
KPageList pageList,
long ownerPid,
MemoryPermission ownerPermission,
MemoryPermission userPermission)
{
2018-12-01 21:24:37 +01:00
_pageList = pageList;
_ownerPid = ownerPid;
_ownerPermission = ownerPermission;
_userPermission = userPermission;
}
public KernelResult MapIntoProcess(
2018-12-01 21:01:59 +01:00
KMemoryManager memoryManager,
ulong address,
ulong size,
KProcess process,
MemoryPermission permission)
{
2018-12-01 21:01:59 +01:00
ulong pagesCountRounded = BitUtils.DivRoundUp(size, KMemoryManager.PageSize);
2018-12-01 21:01:59 +01:00
if (_pageList.GetPagesCount() != pagesCountRounded)
{
return KernelResult.InvalidSize;
}
2018-12-01 21:01:59 +01:00
MemoryPermission expectedPermission = process.Pid == _ownerPid
? _ownerPermission
: _userPermission;
2018-12-01 21:01:59 +01:00
if (permission != expectedPermission)
{
return KernelResult.InvalidPermission;
}
2018-12-01 21:01:59 +01:00
return memoryManager.MapPages(address, _pageList, MemoryState.SharedMemory, permission);
}
public KernelResult UnmapFromProcess(
2018-12-01 21:01:59 +01:00
KMemoryManager memoryManager,
ulong address,
ulong size,
KProcess process)
{
2018-12-01 21:01:59 +01:00
ulong pagesCountRounded = BitUtils.DivRoundUp(size, KMemoryManager.PageSize);
2018-12-01 21:01:59 +01:00
if (_pageList.GetPagesCount() != pagesCountRounded)
{
return KernelResult.InvalidSize;
}
2018-12-01 21:01:59 +01:00
return memoryManager.UnmapPages(address, _pageList, MemoryState.SharedMemory);
}
}
}