2018-12-18 00:33:36 -05:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2023-01-04 17:15:45 -05:00
|
|
|
using Ryujinx.Horizon.Common;
|
2018-09-23 14:11:46 -04:00
|
|
|
using System;
|
2018-03-12 00:04:52 -04:00
|
|
|
|
2018-12-18 00:33:36 -05:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Process
|
2018-03-12 00:04:52 -04:00
|
|
|
{
|
2018-11-28 17:18:09 -05:00
|
|
|
class KHandleTable
|
2018-03-12 00:04:52 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
public const int SelfThreadHandle = (0x1ffff << 15) | 0;
|
|
|
|
public const int SelfProcessHandle = (0x1ffff << 15) | 1;
|
2018-03-12 00:04:52 -04:00
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
private readonly KernelContext _context;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private KHandleEntry[] _table;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private KHandleEntry _tableHead;
|
|
|
|
private KHandleEntry _nextFreeEntry;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private int _activeSlotsCount;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2023-03-17 07:55:19 -04:00
|
|
|
private uint _size;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private ushort _idCounter;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
public KHandleTable(KernelContext context)
|
2018-03-12 00:04:52 -04:00
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
_context = context;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2023-03-17 07:55:19 -04:00
|
|
|
public Result Initialize(uint size)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2023-03-17 07:55:19 -04:00
|
|
|
if (size > 1024)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
return KernelResult.OutOfMemory;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (size < 1)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
size = 1024;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_size = size;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_idCounter = 1;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_table = new KHandleEntry[size];
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_tableHead = new KHandleEntry(0);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KHandleEntry entry = _tableHead;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
for (int index = 0; index < size; index++)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
_table[index] = entry;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
entry.Next = new KHandleEntry(index + 1);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
entry = entry.Next;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_table[size - 1].Next = null;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_nextFreeEntry = _tableHead;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
return Result.Success;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
public Result GenerateHandle(KAutoObject obj, out int handle)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
handle = 0;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_table)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (_activeSlotsCount >= _size)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
|
|
|
return KernelResult.HandleTableFull;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
KHandleEntry entry = _nextFreeEntry;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_nextFreeEntry = entry.Next;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
entry.Obj = obj;
|
|
|
|
entry.HandleId = _idCounter;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_activeSlotsCount++;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
handle = (_idCounter << 15) | entry.Index;
|
|
|
|
|
|
|
|
obj.IncrementReferenceCount();
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((short)(_idCounter + 1) >= 0)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
_idCounter++;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
_idCounter = 1;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
return Result.Success;
|
2018-03-12 00:04:52 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
public Result ReserveHandle(out int handle)
|
2019-01-18 17:26:39 -05:00
|
|
|
{
|
|
|
|
handle = 0;
|
|
|
|
|
|
|
|
lock (_table)
|
|
|
|
{
|
|
|
|
if (_activeSlotsCount >= _size)
|
|
|
|
{
|
|
|
|
return KernelResult.HandleTableFull;
|
|
|
|
}
|
|
|
|
|
|
|
|
KHandleEntry entry = _nextFreeEntry;
|
|
|
|
|
|
|
|
_nextFreeEntry = entry.Next;
|
|
|
|
|
|
|
|
_activeSlotsCount++;
|
|
|
|
|
|
|
|
handle = (_idCounter << 15) | entry.Index;
|
|
|
|
|
|
|
|
if ((short)(_idCounter + 1) >= 0)
|
|
|
|
{
|
|
|
|
_idCounter++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_idCounter = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 17:15:45 -05:00
|
|
|
return Result.Success;
|
2019-01-18 17:26:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CancelHandleReservation(int handle)
|
|
|
|
{
|
2020-05-03 23:41:29 -04:00
|
|
|
int index = (handle >> 0) & 0x7fff;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
lock (_table)
|
|
|
|
{
|
|
|
|
KHandleEntry entry = _table[index];
|
|
|
|
|
|
|
|
entry.Obj = null;
|
|
|
|
entry.Next = _nextFreeEntry;
|
|
|
|
|
|
|
|
_nextFreeEntry = entry;
|
|
|
|
|
|
|
|
_activeSlotsCount--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetReservedHandleObj(int handle, KAutoObject obj)
|
|
|
|
{
|
|
|
|
int index = (handle >> 0) & 0x7fff;
|
|
|
|
int handleId = (handle >> 15);
|
|
|
|
|
|
|
|
lock (_table)
|
|
|
|
{
|
|
|
|
KHandleEntry entry = _table[index];
|
|
|
|
|
|
|
|
entry.Obj = obj;
|
2020-05-03 23:41:29 -04:00
|
|
|
entry.HandleId = (ushort)handleId;
|
2019-01-18 17:26:39 -05:00
|
|
|
|
|
|
|
obj.IncrementReferenceCount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public bool CloseHandle(int handle)
|
2018-03-12 00:04:52 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if ((handle >> 30) != 0 ||
|
|
|
|
handle == SelfThreadHandle ||
|
|
|
|
handle == SelfProcessHandle)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
int index = (handle >> 0) & 0x7fff;
|
|
|
|
int handleId = (handle >> 15);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
KAutoObject obj = null;
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
bool result = false;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_table)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (handleId != 0 && index < _size)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KHandleEntry entry = _table[index];
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if ((obj = entry.Obj) != null && entry.HandleId == handleId)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
entry.Obj = null;
|
|
|
|
entry.Next = _nextFreeEntry;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_nextFreeEntry = entry;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_activeSlotsCount--;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
result = true;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
obj.DecrementReferenceCount();
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return result;
|
2018-03-12 00:04:52 -04:00
|
|
|
}
|
|
|
|
|
2019-01-18 17:26:39 -05:00
|
|
|
public T GetObject<T>(int handle) where T : KAutoObject
|
2018-03-12 00:04:52 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
int index = (handle >> 0) & 0x7fff;
|
|
|
|
int handleId = (handle >> 15);
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_table)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2019-01-18 17:26:39 -05:00
|
|
|
if ((handle >> 30) == 0 && handleId != 0 && index < _size)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KHandleEntry entry = _table[index];
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (entry.HandleId == handleId && entry.Obj is T obj)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return obj;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:41:29 -04:00
|
|
|
return default;
|
2018-03-12 00:04:52 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public KThread GetKThread(int handle)
|
2018-03-12 00:04:52 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (handle == SelfThreadHandle)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2020-12-09 17:20:05 -05:00
|
|
|
return KernelStatic.GetCurrentThread();
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return GetObject<KThread>(handle);
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
2018-03-12 00:04:52 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public KProcess GetKProcess(int handle)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (handle == SelfProcessHandle)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2020-12-09 17:20:05 -05:00
|
|
|
return KernelStatic.GetCurrentProcess();
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return GetObject<KProcess>(handle);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 14:11:46 -04:00
|
|
|
public void Destroy()
|
2018-03-12 00:04:52 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_table)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
for (int index = 0; index < _size; index++)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
KHandleEntry entry = _table[index];
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (entry.Obj != null)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
if (entry.Obj is IDisposable disposableObj)
|
2018-09-23 14:11:46 -04:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
disposableObj.Dispose();
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
|
2019-12-25 20:50:17 -05:00
|
|
|
entry.Obj.DecrementReferenceCount();
|
2018-12-06 06:16:24 -05:00
|
|
|
entry.Obj = null;
|
|
|
|
entry.Next = _nextFreeEntry;
|
2018-09-23 14:11:46 -04:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_nextFreeEntry = entry;
|
2018-09-23 14:11:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-12 00:04:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|