Ejemplo n.º 1
0
        private bool CondVarWait(
            KThread WaitThread,
            int WaitThreadHandle,
            long MutexAddress,
            long CondVarAddress,
            ulong Timeout)
        {
            WaitThread.WaitHandle     = WaitThreadHandle;
            WaitThread.MutexAddress   = MutexAddress;
            WaitThread.CondVarAddress = CondVarAddress;

            lock (Process.ThreadSyncLock)
            {
                MutexUnlock(WaitThread, MutexAddress);

                WaitThread.CondVarSignaled = false;

                Process.ThreadArbiterList.Add(WaitThread);
            }

            Device.Log.PrintDebug(LogClass.KernelSvc, "Entering wait state...");

            if (Timeout != ulong.MaxValue)
            {
                Process.Scheduler.EnterWait(WaitThread, NsTimeConverter.GetTimeMs(Timeout));

                lock (Process.ThreadSyncLock)
                {
                    if (!WaitThread.CondVarSignaled || WaitThread.MutexOwner != null)
                    {
                        if (WaitThread.MutexOwner != null)
                        {
                            WaitThread.MutexOwner.MutexWaiters.Remove(WaitThread);
                            WaitThread.MutexOwner.UpdatePriority();

                            WaitThread.MutexOwner = null;
                        }

                        Process.ThreadArbiterList.Remove(WaitThread);

                        Device.Log.PrintDebug(LogClass.KernelSvc, "Timed out...");

                        return(false);
                    }
                }
            }
            else
            {
                Process.Scheduler.EnterWait(WaitThread);
            }

            return(true);
        }
Ejemplo n.º 2
0
        static ulong WaitForAddress(Process Process, AThreadState ThreadState, long Address, ulong Timeout)
        {
            KThread CurrentThread = Process.GetThread(ThreadState.Tpidr);

            Process.Scheduler.SetReschedule(CurrentThread.ProcessorId);

            CurrentThread.ArbiterWaitAddress = Address;
            CurrentThread.ArbiterSignaled    = false;

            Process.Scheduler.EnterWait(CurrentThread, NsTimeConverter.GetTimeMs(Timeout));

            if (!CurrentThread.ArbiterSignaled)
            {
                return(MakeError(ErrorModule.Kernel, KernelErr.Timeout));
            }

            return(0);
        }
Ejemplo n.º 3
0
        private void SvcSleepThread(AThreadState ThreadState)
        {
            ulong TimeoutNs = ThreadState.X0;

            Device.Log.PrintDebug(LogClass.KernelSvc, "Timeout = 0x" + TimeoutNs.ToString("x16"));

            KThread CurrThread = Process.GetThread(ThreadState.Tpidr);

            if (TimeoutNs == 0 || TimeoutNs == ulong.MaxValue)
            {
                Process.Scheduler.Yield(CurrThread);
            }
            else
            {
                Process.Scheduler.Suspend(CurrThread);

                Thread.Sleep(NsTimeConverter.GetTimeMs(TimeoutNs));

                Process.Scheduler.Resume(CurrThread);
            }
        }
Ejemplo n.º 4
0
        private void SvcWaitSynchronization(AThreadState ThreadState)
        {
            long  HandlesPtr   = (long)ThreadState.X1;
            int   HandlesCount = (int)ThreadState.X2;
            ulong Timeout      = ThreadState.X3;

            Device.Log.PrintDebug(LogClass.KernelSvc,
                                  "HandlesPtr = 0x" + HandlesPtr.ToString("x16") + ", " +
                                  "HandlesCount = 0x" + HandlesCount.ToString("x8") + ", " +
                                  "Timeout = 0x" + Timeout.ToString("x16"));

            if ((uint)HandlesCount > 0x40)
            {
                ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.CountOutOfRange);

                return;
            }

            KThread CurrThread = Process.GetThread(ThreadState.Tpidr);

            WaitHandle[] Handles = new WaitHandle[HandlesCount + 1];

            for (int Index = 0; Index < HandlesCount; Index++)
            {
                int Handle = Memory.ReadInt32(HandlesPtr + Index * 4);

                KSynchronizationObject SyncObj = Process.HandleTable.GetData <KSynchronizationObject>(Handle);

                if (SyncObj == null)
                {
                    Device.Log.PrintWarning(LogClass.KernelSvc, $"Invalid handle 0x{Handle:x8}!");

                    ThreadState.X0 = MakeError(ErrorModule.Kernel, KernelErr.InvalidHandle);

                    return;
                }

                Handles[Index] = SyncObj.WaitEvent;
            }

            using (AutoResetEvent WaitEvent = new AutoResetEvent(false))
            {
                if (!SyncWaits.TryAdd(CurrThread, WaitEvent))
                {
                    throw new InvalidOperationException();
                }

                Handles[HandlesCount] = WaitEvent;

                Process.Scheduler.Suspend(CurrThread);

                int HandleIndex;

                ulong Result = 0;

                if (Timeout != ulong.MaxValue)
                {
                    HandleIndex = WaitHandle.WaitAny(Handles, NsTimeConverter.GetTimeMs(Timeout));
                }
                else
                {
                    HandleIndex = WaitHandle.WaitAny(Handles);
                }

                if (HandleIndex == WaitHandle.WaitTimeout)
                {
                    Result = MakeError(ErrorModule.Kernel, KernelErr.Timeout);
                }
                else if (HandleIndex == HandlesCount)
                {
                    Result = MakeError(ErrorModule.Kernel, KernelErr.Canceled);
                }

                SyncWaits.TryRemove(CurrThread, out _);

                Process.Scheduler.Resume(CurrThread);

                ThreadState.X0 = Result;

                if (Result == 0)
                {
                    ThreadState.X1 = (ulong)HandleIndex;
                }
            }
        }