2018-04-19 04:52:23 +02:00
|
|
|
using ChocolArm64.State;
|
2018-04-24 20:57:39 +02:00
|
|
|
using Ryujinx.Core.Logging;
|
2018-04-19 04:52:23 +02:00
|
|
|
using Ryujinx.Core.OsHle.Handles;
|
2018-04-21 21:07:16 +02:00
|
|
|
using System;
|
2018-05-14 08:01:10 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2018-04-21 21:07:16 +02:00
|
|
|
using System.Threading;
|
2018-04-19 04:52:23 +02:00
|
|
|
|
|
|
|
using static Ryujinx.Core.OsHle.ErrorCode;
|
|
|
|
|
|
|
|
namespace Ryujinx.Core.OsHle.Kernel
|
|
|
|
{
|
|
|
|
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-04-26 04:11:26 +02:00
|
|
|
Ns.Log.PrintDebug(LogClass.KernelSvc,
|
|
|
|
"OwnerThreadHandle = " + OwnerThreadHandle.ToString("x8") + ", " +
|
|
|
|
"MutexAddress = " + MutexAddress .ToString("x16") + ", " +
|
|
|
|
"WaitThreadHandle = " + WaitThreadHandle .ToString("x8"));
|
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
if (IsPointingInsideKernel(MutexAddress))
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{MutexAddress:x16}!");
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsWordAddressUnaligned(MutexAddress))
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{MutexAddress:x16}!");
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAlignment);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-04-19 04:52:23 +02:00
|
|
|
|
|
|
|
KThread OwnerThread = Process.HandleTable.GetData<KThread>(OwnerThreadHandle);
|
|
|
|
|
|
|
|
if (OwnerThread == null)
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.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-04-24 20:57:39 +02:00
|
|
|
Ns.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-04-26 04:11:26 +02:00
|
|
|
Ns.Log.PrintDebug(LogClass.KernelSvc, "MutexAddress = " + MutexAddress.ToString("x16"));
|
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
if (IsPointingInsideKernel(MutexAddress))
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{MutexAddress:x16}!");
|
2018-04-19 04:52:23 +02:00
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsWordAddressUnaligned(MutexAddress))
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{MutexAddress:x16}!");
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAlignment);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MutexUnlock(Process.GetThread(ThreadState.Tpidr), MutexAddress))
|
|
|
|
{
|
|
|
|
Process.Scheduler.Yield(Process.GetThread(ThreadState.Tpidr));
|
|
|
|
}
|
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-04-26 04:11:26 +02:00
|
|
|
Ns.Log.PrintDebug(LogClass.KernelSvc,
|
2018-05-14 08:01:10 +02:00
|
|
|
"MutexAddress = " + MutexAddress .ToString("x16") + ", " +
|
|
|
|
"CondVarAddress = " + CondVarAddress.ToString("x16") + ", " +
|
|
|
|
"ThreadHandle = " + ThreadHandle .ToString("x8") + ", " +
|
|
|
|
"Timeout = " + Timeout .ToString("x16"));
|
2018-04-26 04:11:26 +02:00
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
if (IsPointingInsideKernel(MutexAddress))
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex address 0x{MutexAddress:x16}!");
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAddress);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsWordAddressUnaligned(MutexAddress))
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Unaligned mutex address 0x{MutexAddress:x16}!");
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidAlignment);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
KThread Thread = Process.HandleTable.GetData<KThread>(ThreadHandle);
|
|
|
|
|
|
|
|
if (Thread == null)
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.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-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
|
|
|
MutexUnlock(CurrThread, MutexAddress);
|
2018-04-19 04:52:23 +02:00
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
if (!CondVarWait(CurrThread, ThreadHandle, MutexAddress, CondVarAddress, Timeout))
|
2018-04-19 04:52:23 +02:00
|
|
|
{
|
|
|
|
ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.Timeout);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
Process.Scheduler.Yield(Process.GetThread(ThreadState.Tpidr));
|
2018-04-19 09:06:23 +02:00
|
|
|
|
2018-04-19 04:52:23 +02:00
|
|
|
ThreadState.X0 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SvcSignalProcessWideKey(AThreadState ThreadState)
|
|
|
|
{
|
|
|
|
long CondVarAddress = (long)ThreadState.X0;
|
|
|
|
int Count = (int)ThreadState.X1;
|
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
CondVarSignal(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-04-21 21:07:16 +02:00
|
|
|
int MutexValue = Process.Memory.ReadInt32(MutexAddress);
|
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
Ns.Log.PrintDebug(LogClass.KernelSvc, "MutexValue = " + MutexValue.ToString("x8"));
|
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
lock (Process.ThreadSyncLock)
|
2018-04-19 04:52:23 +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-05-14 08:01:10 +02:00
|
|
|
InsertWaitingMutexThread(OwnerThreadHandle, WaitThread);
|
|
|
|
}
|
2018-04-21 21:07:16 +02:00
|
|
|
|
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
|
|
|
Ns.Log.PrintDebug(LogClass.KernelSvc, "Entering wait state...");
|
|
|
|
|
2018-04-26 04:11:26 +02:00
|
|
|
Process.Scheduler.EnterWait(CurrThread);
|
2018-04-19 04:52:23 +02:00
|
|
|
}
|
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
private bool MutexUnlock(KThread CurrThread, long MutexAddress)
|
2018-04-19 04:52:23 +02:00
|
|
|
{
|
2018-04-21 21:07:16 +02:00
|
|
|
if (CurrThread == null)
|
2018-04-19 04:52:23 +02:00
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid mutex 0x{MutexAddress:x16}!");
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
return false;
|
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
|
|
|
{
|
|
|
|
//This is the new thread that will not own the mutex.
|
|
|
|
//If no threads are waiting for the lock, then it should be null.
|
2018-05-14 08:01:10 +02:00
|
|
|
KThread OwnerThread = GetHighestPriority(CurrThread.MutexWaiters, MutexAddress);
|
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.
|
|
|
|
UpdateMutexOwner(CurrThread, OwnerThread, MutexAddress);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
CurrThread.UpdatePriority();
|
|
|
|
OwnerThread.UpdatePriority();
|
2018-04-26 04:11:26 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
int HasListeners = OwnerThread.MutexWaiters.Count > 0 ? MutexHasListenersMask : 0;
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
Process.Memory.WriteInt32(MutexAddress, HasListeners | OwnerThread.WaitHandle);
|
|
|
|
|
|
|
|
OwnerThread.WaitHandle = 0;
|
|
|
|
OwnerThread.MutexAddress = 0;
|
|
|
|
OwnerThread.CondVarAddress = 0;
|
|
|
|
|
|
|
|
OwnerThread.MutexOwner = null;
|
|
|
|
|
|
|
|
OwnerThread.UpdatePriority();
|
|
|
|
|
|
|
|
Process.Scheduler.WakeUp(OwnerThread);
|
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
Ns.Log.PrintDebug(LogClass.KernelSvc, "Gave mutex to thread id " + OwnerThread.ThreadId + "!");
|
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Process.Memory.WriteInt32(MutexAddress, 0);
|
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
Ns.Log.PrintDebug(LogClass.KernelSvc, "No threads waiting mutex!");
|
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool CondVarWait(
|
|
|
|
KThread WaitThread,
|
|
|
|
int WaitThreadHandle,
|
|
|
|
long MutexAddress,
|
|
|
|
long CondVarAddress,
|
|
|
|
ulong Timeout)
|
|
|
|
{
|
|
|
|
WaitThread.WaitHandle = WaitThreadHandle;
|
|
|
|
WaitThread.MutexAddress = MutexAddress;
|
|
|
|
WaitThread.CondVarAddress = CondVarAddress;
|
|
|
|
|
2018-05-13 05:22:42 +02:00
|
|
|
lock (Process.ThreadArbiterListLock)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-05-14 08:01:10 +02:00
|
|
|
Process.ThreadArbiterList.Add(WaitThread);
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
Ns.Log.PrintDebug(LogClass.KernelSvc, "Entering wait state...");
|
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
if (Timeout != ulong.MaxValue)
|
|
|
|
{
|
|
|
|
return Process.Scheduler.EnterWait(WaitThread, NsTimeConverter.GetTimeMs(Timeout));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-26 04:11:26 +02:00
|
|
|
Process.Scheduler.EnterWait(WaitThread);
|
|
|
|
|
|
|
|
return true;
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CondVarSignal(long CondVarAddress, int Count)
|
|
|
|
{
|
2018-05-13 05:22:42 +02:00
|
|
|
lock (Process.ThreadArbiterListLock)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-05-14 08:01:10 +02:00
|
|
|
KThread CurrThread = GetHighestPriority(CondVarAddress);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
while (CurrThread != null && (Count == -1 || Count-- > 0))
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-05-14 08:01:10 +02:00
|
|
|
AcquireMutexValue(CurrThread.MutexAddress);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
int MutexValue = Process.Memory.ReadInt32(CurrThread.MutexAddress);
|
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.
|
|
|
|
Process.Memory.WriteInt32(CurrThread.MutexAddress, CurrThread.WaitHandle);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
CurrThread.WaitHandle = 0;
|
|
|
|
CurrThread.MutexAddress = 0;
|
|
|
|
CurrThread.CondVarAddress = 0;
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
CurrThread.MutexOwner?.UpdatePriority();
|
2018-04-26 04:11:26 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
CurrThread.MutexOwner = null;
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
Process.Scheduler.WakeUp(CurrThread);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Wait until the lock is released.
|
|
|
|
MutexValue &= ~MutexHasListenersMask;
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
InsertWaitingMutexThread(MutexValue, CurrThread);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
MutexValue |= MutexHasListenersMask;
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
Process.Memory.WriteInt32(CurrThread.MutexAddress, MutexValue);
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
ReleaseMutexValue(CurrThread.MutexAddress);
|
|
|
|
|
|
|
|
CurrThread = GetHighestPriority(CondVarAddress);
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void InsertWaitingMutexThread(int OwnerThreadHandle, KThread WaitThread)
|
|
|
|
{
|
|
|
|
KThread OwnerThread = Process.HandleTable.GetData<KThread>(OwnerThreadHandle);
|
|
|
|
|
|
|
|
if (OwnerThread == null)
|
|
|
|
{
|
2018-04-24 20:57:39 +02:00
|
|
|
Ns.Log.PrintWarning(LogClass.KernelSvc, $"Invalid thread handle 0x{OwnerThreadHandle:x8}!");
|
2018-04-21 21:07:16 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
InsertWaitingMutexThread(OwnerThread, WaitThread);
|
|
|
|
}
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
private void InsertWaitingMutexThread(KThread OwnerThread, KThread WaitThread)
|
|
|
|
{
|
|
|
|
lock (Process.ThreadSyncLock)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-05-14 08:01:10 +02:00
|
|
|
WaitThread.MutexOwner = OwnerThread;
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
if (!OwnerThread.MutexWaiters.Contains(WaitThread))
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-05-14 08:01:10 +02:00
|
|
|
OwnerThread.MutexWaiters.Add(WaitThread);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
OwnerThread.UpdatePriority();
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
2018-05-14 08:01:10 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
private void UpdateMutexOwner(KThread CurrThread, KThread NewOwner, long MutexAddress)
|
|
|
|
{
|
|
|
|
//Go through all threads waiting for the mutex,
|
|
|
|
//and update the MutexOwner field to point to the new owner.
|
|
|
|
lock (Process.ThreadSyncLock)
|
|
|
|
{
|
|
|
|
for (int Index = 0; Index < CurrThread.MutexWaiters.Count; Index++)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-05-14 08:01:10 +02:00
|
|
|
KThread Thread = CurrThread.MutexWaiters[Index];
|
|
|
|
|
|
|
|
if (Thread.MutexAddress == MutexAddress)
|
2018-04-21 21:07:16 +02:00
|
|
|
{
|
2018-05-14 08:01:10 +02:00
|
|
|
CurrThread.MutexWaiters.RemoveAt(Index--);
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
Thread.MutexOwner = NewOwner;
|
|
|
|
|
|
|
|
NewOwner.MutexWaiters.Add(Thread);
|
|
|
|
}
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-14 08:01:10 +02:00
|
|
|
}
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
private KThread GetHighestPriority(List<KThread> Threads, long MutexAddress)
|
|
|
|
{
|
|
|
|
return GetHighestPriority(Threads, x => x.MutexAddress == MutexAddress);
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
private KThread GetHighestPriority(long CondVarAddress)
|
2018-04-26 04:11:26 +02:00
|
|
|
{
|
2018-05-14 08:01:10 +02:00
|
|
|
return GetHighestPriority(Process.ThreadArbiterList, x => x.CondVarAddress == CondVarAddress);
|
|
|
|
}
|
2018-04-26 04:11:26 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
private KThread GetHighestPriority(List<KThread> Threads, Func<KThread, bool> Predicate)
|
|
|
|
{
|
|
|
|
KThread Thread = Threads.OrderBy(x => x.ActualPriority).FirstOrDefault(Predicate);
|
2018-04-26 04:11:26 +02:00
|
|
|
|
2018-05-14 08:01:10 +02:00
|
|
|
if (Thread != null)
|
|
|
|
{
|
|
|
|
Threads.Remove(Thread);
|
2018-04-26 04:11:26 +02:00
|
|
|
}
|
2018-05-14 08:01:10 +02:00
|
|
|
|
|
|
|
return Thread;
|
2018-04-26 04:11:26 +02:00
|
|
|
}
|
|
|
|
|
2018-04-21 21:07:16 +02:00
|
|
|
private void AcquireMutexValue(long MutexAddress)
|
|
|
|
{
|
|
|
|
while (!Process.Memory.AcquireAddress(MutexAddress))
|
|
|
|
{
|
|
|
|
Thread.Yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ReleaseMutexValue(long MutexAddress)
|
|
|
|
{
|
|
|
|
Process.Memory.ReleaseAddress(MutexAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsPointingInsideKernel(long Address)
|
|
|
|
{
|
|
|
|
return ((ulong)Address + 0x1000000000) < 0xffffff000;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsWordAddressUnaligned(long Address)
|
|
|
|
{
|
|
|
|
return (Address & 3) != 0;
|
2018-04-19 04:52:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|