2018-04-19 04:52:23 +02:00
|
|
|
using ChocolArm64.State;
|
2018-06-11 02:46:42 +02:00
|
|
|
using Ryujinx.HLE.Logging;
|
|
|
|
using Ryujinx.HLE.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
|
|
|
|
2018-06-11 02:46:42 +02:00
|
|
|
using static Ryujinx.HLE.OsHle.ErrorCode;
|
2018-04-19 04:52:23 +02:00
|
|
|
|
2018-06-11 02:46:42 +02:00
|
|
|
namespace Ryujinx.HLE.OsHle.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-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;
|
|
|
|
}
|
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadState.X0 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SvcSignalProcessWideKey(AThreadState ThreadState)
|
|
|
|
{
|
|
|
|
long CondVarAddress = (long)ThreadState.X0;
|
|
|
|
int Count = (int)ThreadState.X1;
|
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
Ns.Log.PrintDebug(LogClass.KernelSvc,
|
|
|
|
"CondVarAddress = " + CondVarAddress.ToString("x16") + ", " +
|
|
|
|
"Count = " + Count .ToString("x8"));
|
|
|
|
|
|
|
|
KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
|
|
|
|
|
|
|
|
CondVarSignal(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-05-16 03:36:08 +02:00
|
|
|
int MutexValue = Process.Memory.ReadInt32(MutexAddress);
|
|
|
|
|
|
|
|
Ns.Log.PrintDebug(LogClass.KernelSvc, "MutexValue = " + MutexValue.ToString("x8"));
|
|
|
|
|
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-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-05-16 03:36:08 +02:00
|
|
|
KThread OwnerThread = PopThread(CurrThread.MutexWaiters, x => x.MutexAddress == 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();
|
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
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
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
|
|
|
WaitThread.MutexOwner?.MutexWaiters.Remove(WaitThread);
|
|
|
|
|
|
|
|
if (!WaitThread.CondVarSignaled || WaitThread.MutexOwner != null)
|
2018-05-16 03:36:08 +02:00
|
|
|
{
|
2018-06-10 03:41:07 +02:00
|
|
|
WaitThread.MutexOwner = null;
|
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
Process.ThreadArbiterList.Remove(WaitThread);
|
|
|
|
|
2018-06-10 03:41:07 +02:00
|
|
|
Ns.Log.PrintDebug(LogClass.KernelSvc, "Timed out...");
|
|
|
|
|
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-05-16 03:36:08 +02:00
|
|
|
private void CondVarSignal(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-05-16 03:36:08 +02:00
|
|
|
KThread WaitThread = PopThread(Process.ThreadArbiterList, x => x.CondVarAddress == CondVarAddress);
|
|
|
|
|
|
|
|
if (WaitThread == null)
|
|
|
|
{
|
|
|
|
Ns.Log.PrintDebug(LogClass.KernelSvc, "No more threads to wake up!");
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
WaitThread.CondVarSignaled = true;
|
|
|
|
|
|
|
|
AcquireMutexValue(WaitThread.MutexAddress);
|
|
|
|
|
|
|
|
int MutexValue = Process.Memory.ReadInt32(WaitThread.MutexAddress);
|
|
|
|
|
|
|
|
Ns.Log.PrintDebug(LogClass.KernelSvc, "MutexValue = " + 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-05-16 03:36:08 +02:00
|
|
|
Process.Memory.WriteInt32(WaitThread.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
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Wait until the lock is released.
|
|
|
|
MutexValue &= ~MutexHasListenersMask;
|
2018-04-21 21:07:16 +02:00
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
InsertWaitingMutexThread(MutexValue, WaitThread);
|
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-16 03:36:08 +02:00
|
|
|
Process.Memory.WriteInt32(WaitThread.MutexAddress, MutexValue);
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 03:36:08 +02:00
|
|
|
ReleaseMutexValue(WaitThread.MutexAddress);
|
2018-04-21 21:07:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 08:09:08 +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++)
|
|
|
|
{
|
|
|
|
KThread Thread = CurrThread.MutexWaiters[Index];
|
|
|
|
|
|
|
|
if (Thread.MutexAddress == MutexAddress)
|
|
|
|
{
|
|
|
|
CurrThread.MutexWaiters.RemoveAt(Index--);
|
|
|
|
|
|
|
|
Thread.MutexOwner = NewOwner;
|
|
|
|
|
|
|
|
InsertWaitingMutexThread(NewOwner, Thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-16 03:36:08 +02:00
|
|
|
private KThread PopThread(List<KThread> Threads, Func<KThread, bool> Predicate)
|
2018-05-14 08:01:10 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|