2018-11-28 23:18:09 +01:00
|
|
|
using Ryujinx.Common;
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
|
|
|
class KSharedMemory
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
private KPageList PageList;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private long OwnerPid;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
private MemoryPermission OwnerPermission;
|
|
|
|
private MemoryPermission UserPermission;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
public KSharedMemory(
|
2018-12-05 01:52:39 +01:00
|
|
|
KPageList PageList,
|
|
|
|
long OwnerPid,
|
|
|
|
MemoryPermission OwnerPermission,
|
|
|
|
MemoryPermission UserPermission)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
this.PageList = PageList;
|
|
|
|
this.OwnerPid = OwnerPid;
|
|
|
|
this.OwnerPermission = OwnerPermission;
|
|
|
|
this.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-05 01:52:39 +01:00
|
|
|
KMemoryManager MemoryManager,
|
|
|
|
ulong Address,
|
|
|
|
ulong Size,
|
|
|
|
KProcess Process,
|
|
|
|
MemoryPermission Permission)
|
2018-08-15 20:59:51 +02:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong PagesCountRounded = BitUtils.DivRoundUp(Size, KMemoryManager.PageSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (PageList.GetPagesCount() != PagesCountRounded)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidSize;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
MemoryPermission ExpectedPermission = Process.Pid == OwnerPid
|
|
|
|
? OwnerPermission
|
|
|
|
: UserPermission;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (Permission != ExpectedPermission)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidPermission;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return MemoryManager.MapPages(Address, PageList, MemoryState.SharedMemory, Permission);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult UnmapFromProcess(
|
2018-12-05 01:52:39 +01:00
|
|
|
KMemoryManager MemoryManager,
|
|
|
|
ulong Address,
|
|
|
|
ulong Size,
|
|
|
|
KProcess Process)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
ulong PagesCountRounded = BitUtils.DivRoundUp(Size, KMemoryManager.PageSize);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
if (PageList.GetPagesCount() != PagesCountRounded)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidSize;
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:52:39 +01:00
|
|
|
return MemoryManager.UnmapPages(Address, PageList, MemoryState.SharedMemory);
|
2018-08-15 20:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|