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