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