2018-11-28 23:18:09 +01:00
|
|
|
using Ryujinx.Common;
|
2018-12-18 06:33:36 +01:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-18 06:33:36 +01:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2019-01-18 23:26:39 +01:00
|
|
|
class KSharedMemory : KAutoObject
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
private readonly KPageList _pageList;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
private readonly long _ownerPid;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
private readonly KMemoryPermission _ownerPermission;
|
|
|
|
private readonly KMemoryPermission _userPermission;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
public KSharedMemory(
|
2020-05-04 05:41:29 +02:00
|
|
|
KernelContext context,
|
2018-12-06 12:16:24 +01:00
|
|
|
KPageList pageList,
|
|
|
|
long ownerPid,
|
2020-12-02 00:23:43 +01:00
|
|
|
KMemoryPermission ownerPermission,
|
|
|
|
KMemoryPermission userPermission) : base(context)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_pageList = pageList;
|
|
|
|
_ownerPid = ownerPid;
|
|
|
|
_ownerPermission = ownerPermission;
|
|
|
|
_userPermission = userPermission;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-08-15 20:59:51 +02:00
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
public KernelResult MapIntoProcess(
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryManager memoryManager,
|
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
KProcess process,
|
2020-12-02 00:23:43 +01:00
|
|
|
KMemoryPermission permission)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCountRounded = BitUtils.DivRoundUp(size, KMemoryManager.PageSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (_pageList.GetPagesCount() != pagesCountRounded)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidSize;
|
|
|
|
}
|
|
|
|
|
2020-12-02 00:23:43 +01:00
|
|
|
KMemoryPermission expectedPermission = process.Pid == _ownerPid
|
2018-12-06 12:16:24 +01:00
|
|
|
? _ownerPermission
|
|
|
|
: _userPermission;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (permission != expectedPermission)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidPermission;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return memoryManager.MapPages(address, _pageList, MemoryState.SharedMemory, permission);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult UnmapFromProcess(
|
2018-12-06 12:16:24 +01:00
|
|
|
KMemoryManager memoryManager,
|
|
|
|
ulong address,
|
|
|
|
ulong size,
|
|
|
|
KProcess process)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
ulong pagesCountRounded = BitUtils.DivRoundUp(size, KMemoryManager.PageSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (_pageList.GetPagesCount() != pagesCountRounded)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidSize;
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return memoryManager.UnmapPages(address, _pageList, MemoryState.SharedMemory);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|