2018-11-28 17:18:09 -05:00
|
|
|
using Ryujinx.Common;
|
2018-12-18 00:33:36 -05:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-18 00:33:36 -05:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
|
|
|
class KSharedMemory
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
private KPageList _pageList;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private long _ownerPid;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private MemoryPermission _ownerPermission;
|
|
|
|
private MemoryPermission _userPermission;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
public KSharedMemory(
|
2018-12-06 06:16:24 -05:00
|
|
|
KPageList pageList,
|
|
|
|
long ownerPid,
|
|
|
|
MemoryPermission ownerPermission,
|
|
|
|
MemoryPermission userPermission)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
_pageList = pageList;
|
|
|
|
_ownerPid = ownerPid;
|
|
|
|
_ownerPermission = ownerPermission;
|
|
|
|
_userPermission = userPermission;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-08-15 14:59:51 -04:00
|
|
|
|
2018-11-28 17:18:09 -05:00
|
|
|
public KernelResult MapIntoProcess(
|
2018-12-06 06:16:24 -05:00
|
|
|
KMemoryManager memoryManager,
|
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
KProcess process,
|
|
|
|
MemoryPermission permission)
|
2018-08-15 14:59:51 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong pagesCountRounded = BitUtils.DivRoundUp(size, KMemoryManager.PageSize);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (_pageList.GetPagesCount() != pagesCountRounded)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidSize;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
MemoryPermission expectedPermission = process.Pid == _ownerPid
|
|
|
|
? _ownerPermission
|
|
|
|
: _userPermission;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (permission != expectedPermission)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidPermission;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return memoryManager.MapPages(address, _pageList, MemoryState.SharedMemory, permission);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult UnmapFromProcess(
|
2018-12-06 06:16:24 -05:00
|
|
|
KMemoryManager memoryManager,
|
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
KProcess process)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ulong pagesCountRounded = BitUtils.DivRoundUp(size, KMemoryManager.PageSize);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (_pageList.GetPagesCount() != pagesCountRounded)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidSize;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return memoryManager.UnmapPages(address, _pageList, MemoryState.SharedMemory);
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|