2022-05-03 11:16:31 +00:00
|
|
|
using Ryujinx.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
2023-01-04 22:15:45 +00:00
|
|
|
using Ryujinx.Horizon.Common;
|
2022-05-03 11:16:31 +00:00
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
|
|
|
{
|
|
|
|
class KCodeMemory : KAutoObject
|
|
|
|
{
|
|
|
|
public KProcess Owner { get; private set; }
|
|
|
|
private readonly KPageList _pageList;
|
|
|
|
private readonly object _lock;
|
|
|
|
private ulong _address;
|
|
|
|
private bool _isOwnerMapped;
|
|
|
|
private bool _isMapped;
|
|
|
|
|
|
|
|
public KCodeMemory(KernelContext context) : base(context)
|
|
|
|
{
|
|
|
|
_pageList = new KPageList();
|
|
|
|
_lock = new object();
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
public Result Initialize(ulong address, ulong size)
|
2022-05-03 11:16:31 +00:00
|
|
|
{
|
|
|
|
Owner = KernelStatic.GetCurrentProcess();
|
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
Result result = Owner.MemoryManager.BorrowCodeMemory(_pageList, address, size);
|
2022-05-03 11:16:31 +00:00
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
if (result != Result.Success)
|
2022-05-03 11:16:31 +00:00
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Owner.CpuMemory.Fill(address, size, 0xff);
|
|
|
|
Owner.IncrementReferenceCount();
|
|
|
|
|
|
|
|
_address = address;
|
|
|
|
_isMapped = false;
|
|
|
|
_isOwnerMapped = false;
|
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
return Result.Success;
|
2022-05-03 11:16:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
public Result Map(ulong address, ulong size, KMemoryPermission perm)
|
2022-05-03 11:16:31 +00:00
|
|
|
{
|
2022-12-26 14:11:05 +00:00
|
|
|
if (_pageList.GetPagesCount() != BitUtils.DivRoundUp<ulong>(size, (ulong)KPageTableBase.PageSize))
|
2022-05-03 11:16:31 +00:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
if (_isMapped)
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidState;
|
|
|
|
}
|
|
|
|
|
|
|
|
KProcess process = KernelStatic.GetCurrentProcess();
|
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
Result result = process.MemoryManager.MapPages(address, _pageList, MemoryState.CodeWritable, KMemoryPermission.ReadAndWrite);
|
2022-05-03 11:16:31 +00:00
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
if (result != Result.Success)
|
2022-05-03 11:16:31 +00:00
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isMapped = true;
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
return Result.Success;
|
2022-05-03 11:16:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
public Result MapToOwner(ulong address, ulong size, KMemoryPermission permission)
|
2022-05-03 11:16:31 +00:00
|
|
|
{
|
2022-12-26 14:11:05 +00:00
|
|
|
if (_pageList.GetPagesCount() != BitUtils.DivRoundUp<ulong>(size, (ulong)KPageTableBase.PageSize))
|
2022-05-03 11:16:31 +00:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
if (_isOwnerMapped)
|
|
|
|
{
|
|
|
|
return KernelResult.InvalidState;
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug.Assert(permission == KMemoryPermission.Read || permission == KMemoryPermission.ReadAndExecute);
|
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
Result result = Owner.MemoryManager.MapPages(address, _pageList, MemoryState.CodeReadOnly, permission);
|
2022-05-03 11:16:31 +00:00
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
if (result != Result.Success)
|
2022-05-03 11:16:31 +00:00
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isOwnerMapped = true;
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
return Result.Success;
|
2022-05-03 11:16:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
public Result Unmap(ulong address, ulong size)
|
2022-05-03 11:16:31 +00:00
|
|
|
{
|
2022-12-26 14:11:05 +00:00
|
|
|
if (_pageList.GetPagesCount() != BitUtils.DivRoundUp<ulong>(size, (ulong)KPageTableBase.PageSize))
|
2022-05-03 11:16:31 +00:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (_lock)
|
|
|
|
{
|
|
|
|
KProcess process = KernelStatic.GetCurrentProcess();
|
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
Result result = process.MemoryManager.UnmapPages(address, _pageList, MemoryState.CodeWritable);
|
2022-05-03 11:16:31 +00:00
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
if (result != Result.Success)
|
2022-05-03 11:16:31 +00:00
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug.Assert(_isMapped);
|
|
|
|
|
|
|
|
_isMapped = false;
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
return Result.Success;
|
2022-05-03 11:16:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
public Result UnmapFromOwner(ulong address, ulong size)
|
2022-05-03 11:16:31 +00:00
|
|
|
{
|
2022-12-26 14:11:05 +00:00
|
|
|
if (_pageList.GetPagesCount() != BitUtils.DivRoundUp<ulong>(size, KPageTableBase.PageSize))
|
2022-05-03 11:16:31 +00:00
|
|
|
{
|
|
|
|
return KernelResult.InvalidSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (_lock)
|
|
|
|
{
|
2023-01-04 22:15:45 +00:00
|
|
|
Result result = Owner.MemoryManager.UnmapPages(address, _pageList, MemoryState.CodeReadOnly);
|
2022-05-03 11:16:31 +00:00
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
if (result != Result.Success)
|
2022-05-03 11:16:31 +00:00
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug.Assert(_isOwnerMapped);
|
|
|
|
|
|
|
|
_isOwnerMapped = false;
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
return Result.Success;
|
2022-05-03 11:16:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Destroy()
|
|
|
|
{
|
|
|
|
if (!_isMapped && !_isOwnerMapped)
|
|
|
|
{
|
|
|
|
ulong size = _pageList.GetPagesCount() * KPageTableBase.PageSize;
|
|
|
|
|
2023-01-04 22:15:45 +00:00
|
|
|
if (Owner.MemoryManager.UnborrowCodeMemory(_address, size, _pageList) != Result.Success)
|
2022-05-03 11:16:31 +00:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Unexpected failure restoring transfer memory attributes.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Owner.DecrementReferenceCount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|