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