2018-04-19 04:52:23 +02:00
|
|
|
using ChocolArm64.State;
|
2018-06-11 02:46:42 +02:00
|
|
|
using Ryujinx.HLE.Logging;
|
2018-04-21 21:07:16 +02:00
|
|
|
using System;
|
2018-04-19 04:52:23 +02:00
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
using static Ryujinx.HLE.HOS.ErrorCode;
|
2018-04-19 04:52:23 +02:00
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
2018-04-19 04:52:23 +02:00
|
|
|
{
|
|
|
|
partial class SvcHandler
|
|
|
|
{
|
2018-04-21 21:07:16 +02:00
|
|
|
private const int MutexHasListenersMask = 0x40000000;
|
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
private void SvcArbitrateLock(AThreadState ThreadState)
|
|
|
|
{
|
2018-04-21 21:07:16 +02:00
|
|
|
int OwnerThreadHandle = (int)ThreadState.X0;
|
|
|
|
long MutexAddress = (long)ThreadState.X1;
|
|
|
|
int WaitThreadHandle = (int)ThreadState.X2;
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintDebug(LogClass.KernelSvc,
|
|
|
|
"OwnerThreadHandle = 0x" + OwnerThreadHandle.ToString("x8") + ", " +
|
|
|
|
"MutexAddress = 0x" + MutexAddress .ToString("x16") + ", " +
|
|
|
|
"WaitThreadHandle = 0x" + WaitThreadHandle .ToString("x8"));
|
2018-04-26 04:11:26 +02:00
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
if (IsPointingInsideKernel(MutexAddress))
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{MutexAddress:x16}!");
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
if (IsAddressNotWordAligned(MutexAddress))
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{MutexAddress:x16}!");
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-04-19 04:52:23 +02:00
|
|
|
|
|
|
|
KThread OwnerThread = Process.HandleTable.GetData<KThread>(OwnerThreadHandle);
|
|
|
|
|
|
|
|
if (OwnerThread == null)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid owner thread handle 0x{OwnerThreadHandle:x8}!");
|
2018-04-19 04:52:23 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
KThread WaitThread = Process.HandleTable.GetData<KThread>(WaitThreadHandle);
|
2018-04-19 04:52:23 +02:00
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
if (WaitThread == null)
|
2018-04-19 04:52:23 +02:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid requesting thread handle 0x{WaitThreadHandle:x8}!");
|
2018-04-19 04:52:23 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
|
2018-04-19 04:52:23 +02:00
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
MutexLock(CurrThread, WaitThread, OwnerThreadHandle, WaitThreadHandle, MutexAddress);
|
2018-04-19 04:52:23 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SvcArbitrateUnlock(AThreadState ThreadState)
|
|
|
|
{
|
|
|
|
long MutexAddress = (long)ThreadState.X0;
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintDebug(LogClass.KernelSvc, "MutexAddress = 0x" + MutexAddress.ToString("x16"));
|
2018-04-26 04:11:26 +02:00
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
if (IsPointingInsideKernel(MutexAddress))
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{MutexAddress:x16}!");
|
2018-04-19 04:52:23 +02:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
if (IsAddressNotWordAligned(MutexAddress))
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{MutexAddress:x16}!");
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
MutexUnlock(Process.GetThread(ThreadState.Tpidr), MutexAddress);
|
2018-04-19 04:52:23 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SvcWaitProcessWideKeyAtomic(AThreadState ThreadState)
|
|
|
|
{
|
2018-04-19 09:06:23 +02:00
|
|
|
long MutexAddress = (long)ThreadState.X0;
|
|
|
|
long CondVarAddress = (long)ThreadState.X1;
|
|
|
|
int ThreadHandle = (int)ThreadState.X2;
|
|
|
|
ulong Timeout = ThreadState.X3;
|
2018-04-19 04:52:23 +02:00
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintDebug(LogClass.KernelSvc,
|
|
|
|
"MutexAddress = 0x" + MutexAddress .ToString("x16") + ", " +
|
|
|
|
"CondVarAddress = 0x" + CondVarAddress.ToString("x16") + ", " +
|
|
|
|
"ThreadHandle = 0x" + ThreadHandle .ToString("x8") + ", " +
|
|
|
|
"Timeout = 0x" + Timeout .ToString("x16"));
|
2018-04-26 04:11:26 +02:00
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
if (IsPointingInsideKernel(MutexAddress))
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{MutexAddress:x16}!");
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
if (IsAddressNotWordAligned(MutexAddress))
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{MutexAddress:x16}!");
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
KThread Thread = Process.HandleTable.GetData<KThread>(ThreadHandle);
|
|
|
|
|
|
|
|
if (Thread == null)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{ThreadHandle:x8}!");
|
2018-04-19 04:52:23 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);
|
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-04-19 09:06:23 +02:00
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
KThread WaitThread = Process.GetThread(ThreadState.Tpidr);
|
2018-04-19 04:52:23 +02:00
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
if (!CondVarWait(WaitThread, ThreadHandle, MutexAddress, CondVarAddress, Timeout))
|
2018-04-19 04:52:23 +02:00
|
|
|
{
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadState.X0 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SvcSignalProcessWideKey(AThreadState ThreadState)
|
|
|
|
{
|
|
|
|
long CondVarAddress = (long)ThreadState.X0;
|
|
|
|
int Count = (int)ThreadState.X1;
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintDebug(LogClass.KernelSvc,
|
|
|
|
"CondVarAddress = 0x" + CondVarAddress.ToString("x16") + ", " +
|
|
|
|
"Count = 0x" + Count .ToString("x8"));
|
2018-05-16 03:36:08 +02:00
|
|
|
|
|
|
|
KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
|
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
CondVarSignal(ThreadState, CurrThread, CondVarAddress, Count);
|
2018-04-19 04:52:23 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = 0;
|
|
|
|
}
|
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
private void MutexLock(
|
|
|
|
KThread CurrThread,
|
|
|
|
KThread WaitThread,
|
|
|
|
int OwnerThreadHandle,
|
|
|
|
int WaitThreadHandle,
|
|
|
|
long MutexAddress)
|
2018-04-19 04:52:23 +02:00
|
|
|
{
|
2018-05-14 08:01:10 +02:00
|
|
|
lock (Process.ThreadSyncLock)
|
2018-04-19 04:52:23 +02:00
|
|
|
{
|
2018-06-22 04:05:42 +02:00
|
|
|
int MutexValue = Memory.ReadInt32(MutexAddress);
|
2018-05-16 03:36:08 +02:00
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintDebug(LogClass.KernelSvc, "MutexValue = 0x" + MutexValue.ToString("x8"));
|
2018-05-16 03:36:08 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
if (MutexValue != (OwnerThreadHandle | MutexHasListenersMask))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2018-04-19 04:52:23 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
CurrThread.WaitHandle = WaitThreadHandle;
|
|
|
|
CurrThread.MutexAddress = MutexAddress;
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
InsertWaitingMutexThreadUnsafe(OwnerThreadHandle, WaitThread);
|
2018-05-14 08:01:10 +02:00
|
|
|
}
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintDebug(LogClass.KernelSvc, "Entering wait state...");
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 20:53:23 +02:00
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
Process.Scheduler.EnterWait(CurrThread);
|
2018-04-19 04:52:23 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 06:03:53 +02:00
|
|
|
private void SvcWaitForAddress(AThreadState ThreadState)
|
|
|
|
{
|
|
|
|
long Address = (long)ThreadState.X0;
|
|
|
|
ArbitrationType Type = (ArbitrationType)ThreadState.X1;
|
|
|
|
int Value = (int)ThreadState.X2;
|
|
|
|
ulong Timeout = ThreadState.X3;
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintDebug(LogClass.KernelSvc,
|
|
|
|
"Address = 0x" + Address.ToString("x16") + ", " +
|
|
|
|
"ArbitrationType = 0x" + Type .ToString() + ", " +
|
|
|
|
"Value = 0x" + Value .ToString("x8") + ", " +
|
|
|
|
"Timeout = 0x" + Timeout.ToString("x16"));
|
2018-07-19 06:03:53 +02:00
|
|
|
|
|
|
|
if (IsPointingInsideKernel(Address))
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid address 0x{Address:x16}!");
|
2018-07-19 06:03:53 +02:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.NoAccessPerm);
|
2018-07-19 06:03:53 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
if (IsAddressNotWordAligned(Address))
|
2018-07-19 06:03:53 +02:00
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned address 0x{Address:x16}!");
|
2018-07-19 06:03:53 +02:00
|
|
|
|
2018-08-15 20:59:51 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
2018-07-19 06:03:53 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Type)
|
|
|
|
{
|
|
|
|
case ArbitrationType.WaitIfLessThan:
|
|
|
|
ThreadState.X0 = AddressArbiter.WaitForAddressIfLessThan(Process, ThreadState, Memory, Address, Value, Timeout, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ArbitrationType.DecrementAndWaitIfLessThan:
|
|
|
|
ThreadState.X0 = AddressArbiter.WaitForAddressIfLessThan(Process, ThreadState, Memory, Address, Value, Timeout, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ArbitrationType.WaitIfEqual:
|
|
|
|
ThreadState.X0 = AddressArbiter.WaitForAddressIfEqual(Process, ThreadState, Memory, Address, Value, Timeout);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidEnumValue);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
private void MutexUnlock(KThread CurrThread, long MutexAddress)
|
2018-04-19 04:52:23 +02:00
|
|
|
{
|
2018-05-14 08:01:10 +02:00
|
|
|
lock (Process.ThreadSyncLock)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-06-09 23:19:14 +02:00
|
|
|
//This is the new thread that will now own the mutex.
|
2018-04-21 21:07:16 +02:00
|
|
|
//If no threads are waiting for the lock, then it should be null.
|
2018-06-22 04:05:42 +02:00
|
|
|
(KThread OwnerThread, int Count) = PopMutexThreadUnsafe(CurrThread, MutexAddress);
|
|
|
|
|
|
|
|
if (OwnerThread == CurrThread)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
if (OwnerThread != null)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-05-14 08:01:10 +02:00
|
|
|
//Remove all waiting mutex from the old owner,
|
|
|
|
//and insert then on the new owner.
|
2018-06-22 04:05:42 +02:00
|
|
|
UpdateMutexOwnerUnsafe(CurrThread, OwnerThread, MutexAddress);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
CurrThread.UpdatePriority();
|
2018-04-26 04:11:26 +02:00
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
int HasListeners = Count >= 2 ? MutexHasListenersMask : 0;
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
Memory.WriteInt32ToSharedAddr(MutexAddress, HasListeners | OwnerThread.WaitHandle);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
OwnerThread.WaitHandle = 0;
|
|
|
|
OwnerThread.MutexAddress = 0;
|
|
|
|
OwnerThread.CondVarAddress = 0;
|
2018-06-22 04:05:42 +02:00
|
|
|
OwnerThread.MutexOwner = null;
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
OwnerThread.UpdatePriority();
|
|
|
|
|
|
|
|
Process.Scheduler.WakeUp(OwnerThread);
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintDebug(LogClass.KernelSvc, "Gave mutex to thread id " + OwnerThread.ThreadId + "!");
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-22 04:05:42 +02:00
|
|
|
Memory.WriteInt32ToSharedAddr(MutexAddress, 0);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintDebug(LogClass.KernelSvc, "No threads waiting mutex!");
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool CondVarWait(
|
|
|
|
KThread WaitThread,
|
|
|
|
int WaitThreadHandle,
|
|
|
|
long MutexAddress,
|
|
|
|
long CondVarAddress,
|
|
|
|
ulong Timeout)
|
|
|
|
{
|
|
|
|
WaitThread.WaitHandle = WaitThreadHandle;
|
|
|
|
WaitThread.MutexAddress = MutexAddress;
|
|
|
|
WaitThread.CondVarAddress = CondVarAddress;
|
|
|
|
|
2018-06-10 03:41:07 +02:00
|
|
|
lock (Process.ThreadSyncLock)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-06-22 04:05:42 +02:00
|
|
|
MutexUnlock(WaitThread, MutexAddress);
|
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
WaitThread.CondVarSignaled = false;
|
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
Process.ThreadArbiterList.Add(WaitThread);
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintDebug(LogClass.KernelSvc, "Entering wait state...");
|
NvServices refactoring (#120)
* Initial implementation of NvMap/NvHostCtrl
* More work on NvHostCtrl
* Refactoring of nvservices, move GPU Vmm, make Vmm per-process, refactor most gpu devices, move Gpu to Core, fix CbBind
* Implement GetGpuTime, support CancelSynchronization, fix issue on InsertWaitingMutex, proper double buffering support (again, not working properly for commercial games, only hb)
* Try to fix perf regression reading/writing textures, moved syncpts and events to a UserCtx class, delete global state when the process exits, other minor tweaks
* Remove now unused code, add comment about probably wrong result codes
2018-05-07 20:53:23 +02:00
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
if (Timeout != ulong.MaxValue)
|
|
|
|
{
|
2018-05-16 03:36:08 +02:00
|
|
|
Process.Scheduler.EnterWait(WaitThread, NsTimeConverter.GetTimeMs(Timeout));
|
|
|
|
|
2018-06-10 03:41:07 +02:00
|
|
|
lock (Process.ThreadSyncLock)
|
2018-05-16 03:36:08 +02:00
|
|
|
{
|
2018-06-10 03:41:07 +02:00
|
|
|
if (!WaitThread.CondVarSignaled || WaitThread.MutexOwner != null)
|
2018-05-16 03:36:08 +02:00
|
|
|
{
|
2018-06-22 04:05:42 +02:00
|
|
|
if (WaitThread.MutexOwner != null)
|
|
|
|
{
|
|
|
|
WaitThread.MutexOwner.MutexWaiters.Remove(WaitThread);
|
|
|
|
WaitThread.MutexOwner.UpdatePriority();
|
|
|
|
|
|
|
|
WaitThread.MutexOwner = null;
|
|
|
|
}
|
2018-06-10 03:41:07 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
Process.ThreadArbiterList.Remove(WaitThread);
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintDebug(LogClass.KernelSvc, "Timed out...");
|
2018-06-10 03:41:07 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-26 04:11:26 +02:00
|
|
|
Process.Scheduler.EnterWait(WaitThread);
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
2018-05-16 03:36:08 +02:00
|
|
|
|
|
|
|
return true;
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
private void CondVarSignal(
|
|
|
|
AThreadState ThreadState,
|
|
|
|
KThread CurrThread,
|
|
|
|
long CondVarAddress,
|
|
|
|
int Count)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-06-10 03:41:07 +02:00
|
|
|
lock (Process.ThreadSyncLock)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-05-16 03:36:08 +02:00
|
|
|
while (Count == -1 || Count-- > 0)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-06-22 04:05:42 +02:00
|
|
|
KThread WaitThread = PopCondVarThreadUnsafe(CondVarAddress);
|
2018-05-16 03:36:08 +02:00
|
|
|
|
|
|
|
if (WaitThread == null)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintDebug(LogClass.KernelSvc, "No more threads to wake up!");
|
2018-05-16 03:36:08 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
WaitThread.CondVarSignaled = true;
|
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
long MutexAddress = WaitThread.MutexAddress;
|
|
|
|
|
|
|
|
Memory.SetExclusive(ThreadState, MutexAddress);
|
|
|
|
|
|
|
|
int MutexValue = Memory.ReadInt32(MutexAddress);
|
|
|
|
|
|
|
|
while (MutexValue != 0)
|
|
|
|
{
|
|
|
|
if (Memory.TestExclusive(ThreadState, MutexAddress))
|
|
|
|
{
|
|
|
|
//Wait until the lock is released.
|
|
|
|
InsertWaitingMutexThreadUnsafe(MutexValue & ~MutexHasListenersMask, WaitThread);
|
|
|
|
|
|
|
|
Memory.WriteInt32(MutexAddress, MutexValue | MutexHasListenersMask);
|
2018-05-16 03:36:08 +02:00
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
Memory.ClearExclusiveForStore(ThreadState);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Memory.SetExclusive(ThreadState, MutexAddress);
|
|
|
|
|
|
|
|
MutexValue = Memory.ReadInt32(MutexAddress);
|
|
|
|
}
|
2018-05-16 03:36:08 +02:00
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintDebug(LogClass.KernelSvc, "MutexValue = 0x" + MutexValue.ToString("x8"));
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
if (MutexValue == 0)
|
|
|
|
{
|
|
|
|
//Give the lock to this thread.
|
2018-06-22 04:05:42 +02:00
|
|
|
Memory.WriteInt32ToSharedAddr(MutexAddress, WaitThread.WaitHandle);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
WaitThread.WaitHandle = 0;
|
|
|
|
WaitThread.MutexAddress = 0;
|
|
|
|
WaitThread.CondVarAddress = 0;
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
WaitThread.MutexOwner?.UpdatePriority();
|
2018-04-26 04:11:26 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
WaitThread.MutexOwner = null;
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
Process.Scheduler.WakeUp(WaitThread);
|
2018-05-14 08:01:10 +02:00
|
|
|
}
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
private void UpdateMutexOwnerUnsafe(KThread CurrThread, KThread NewOwner, long MutexAddress)
|
2018-05-14 08:09:08 +02:00
|
|
|
{
|
|
|
|
//Go through all threads waiting for the mutex,
|
|
|
|
//and update the MutexOwner field to point to the new owner.
|
2018-06-22 04:05:42 +02:00
|
|
|
for (int Index = 0; Index < CurrThread.MutexWaiters.Count; Index++)
|
2018-05-14 08:09:08 +02:00
|
|
|
{
|
2018-06-22 04:05:42 +02:00
|
|
|
KThread Thread = CurrThread.MutexWaiters[Index];
|
2018-05-14 08:09:08 +02:00
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
if (Thread.MutexAddress == MutexAddress)
|
|
|
|
{
|
|
|
|
CurrThread.MutexWaiters.RemoveAt(Index--);
|
2018-05-14 08:09:08 +02:00
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
InsertWaitingMutexThreadUnsafe(NewOwner, Thread);
|
2018-05-14 08:09:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
private void InsertWaitingMutexThreadUnsafe(int OwnerThreadHandle, KThread WaitThread)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
|
|
|
KThread OwnerThread = Process.HandleTable.GetData<KThread>(OwnerThreadHandle);
|
|
|
|
|
|
|
|
if (OwnerThread == null)
|
|
|
|
{
|
2018-08-17 01:47:36 +02:00
|
|
|
Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{OwnerThreadHandle:x8}!");
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
InsertWaitingMutexThreadUnsafe(OwnerThread, WaitThread);
|
2018-05-14 08:01:10 +02:00
|
|
|
}
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
private void InsertWaitingMutexThreadUnsafe(KThread OwnerThread, KThread WaitThread)
|
2018-05-14 08:01:10 +02:00
|
|
|
{
|
2018-06-22 04:05:42 +02:00
|
|
|
WaitThread.MutexOwner = OwnerThread;
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
if (!OwnerThread.MutexWaiters.Contains(WaitThread))
|
|
|
|
{
|
|
|
|
OwnerThread.MutexWaiters.Add(WaitThread);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
OwnerThread.UpdatePriority();
|
2018-05-14 08:01:10 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
private (KThread, int) PopMutexThreadUnsafe(KThread OwnerThread, long MutexAddress)
|
2018-05-14 08:01:10 +02:00
|
|
|
{
|
2018-06-22 04:05:42 +02:00
|
|
|
int Count = 0;
|
2018-04-26 04:11:26 +02:00
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
KThread WakeThread = null;
|
|
|
|
|
|
|
|
foreach (KThread Thread in OwnerThread.MutexWaiters)
|
2018-05-14 08:01:10 +02:00
|
|
|
{
|
2018-06-22 04:05:42 +02:00
|
|
|
if (Thread.MutexAddress != MutexAddress)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WakeThread == null || Thread.ActualPriority < WakeThread.ActualPriority)
|
|
|
|
{
|
|
|
|
WakeThread = Thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
Count++;
|
2018-04-26 04:11:26 +02:00
|
|
|
}
|
2018-05-14 08:01:10 +02:00
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
if (WakeThread != null)
|
|
|
|
{
|
|
|
|
OwnerThread.MutexWaiters.Remove(WakeThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (WakeThread, Count);
|
2018-04-26 04:11:26 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
private KThread PopCondVarThreadUnsafe(long CondVarAddress)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-06-22 04:05:42 +02:00
|
|
|
KThread WakeThread = null;
|
|
|
|
|
|
|
|
foreach (KThread Thread in Process.ThreadArbiterList)
|
|
|
|
{
|
|
|
|
if (Thread.CondVarAddress != CondVarAddress)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WakeThread == null || Thread.ActualPriority < WakeThread.ActualPriority)
|
|
|
|
{
|
|
|
|
WakeThread = Thread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WakeThread != null)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-06-22 04:05:42 +02:00
|
|
|
Process.ThreadArbiterList.Remove(WakeThread);
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 04:05:42 +02:00
|
|
|
return WakeThread;
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsPointingInsideKernel(long Address)
|
|
|
|
{
|
|
|
|
return ((ulong)Address + 0x1000000000) < 0xffffff000;
|
|
|
|
}
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
private bool IsAddressNotWordAligned(long Address)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
|
|
|
return (Address & 3) != 0;
|
2018-04-19 04:52:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|