Commit Graph

20 Commits

Author SHA1 Message Date
PixelyIon
a74eb8dbd6 Fix Scheduler Core Migration Deadlock
Encountered in 不如帰大乱 when `HOS-3` is awoken at the same time as `HOS-0` called `SvcSetThreadCoreMask` resulting in a deadlock where `HOS-0` owns `HOS-3`'s `coreMigrationMutex` while `HOS-3` owns the core mutex with the both of them attempting to lock the other mutex
2021-07-12 20:57:05 +05:30
PixelyIon
d595b9fe4f Implement SvcUnmapSharedMemory + Remove Redundant [[unlikely]] 2021-07-04 05:58:55 +05:30
Billy Laws
100cff7692 Address feedback 2021-06-20 14:18:40 +01:00
Billy Laws
b87f451746 Handle empty core queues in Scheduler::UpdatePriority
If the queue is empty then begin() == end() leading to
ArmPreemptionTimer being called on an unscheduled thread.
2021-06-20 14:18:40 +01:00
PixelyIon
ae68009f9b Extend Perfetto Tracing
Add Tracing for SVCs, Services, NVDRV, and Synchronization Primitives. In addition, fix `TRACE_EVENT_END("guest")` being emitted when a signal is received while being in the guest rather than host which would cause an exception. This commit also disables warnings for the Perfetto library as we do not control fixing them.
2021-03-23 02:40:02 +05:30
Billy Laws
6c6e665569 Implement Perfetto Tracing
This commit implements tracing Skyline using https://perfetto.dev/ for SVCs, IPC, Scheduler and Presentation
2021-03-23 02:40:02 +05:30
PixelyIon
c282276b74 Address CR Comments 2 (#132) + Fix Several Scheduler Bugs
The following scheduler bugs were fixed:
* It was assumed that all non-cooperative `Rotate` calls were from a preemptive yield and changed the state of `KThread::isPreempted` incorrectly which could lead to UB, an example of a scenario with it would be:
* * Preemptive thread A gets a signal to yield from cooperative thread B due to it being ready to schedule and higher priority
* * A complies with this request but there's an assumption that the signal was actually from it's preemption timer therefore it doesn't reset it (As it isn't required if the timer was responsible for the signal)
* * A receives the actual preemption signal a while later, causing UB as the signal handler is invoked twice
* `Scheduler::UpdatePriority`
* * A check for `currentIt == core->queue.begin()` existed which caused an incorrect early return 
* * The preemption timer was armed correctly when a priority transition from cooperative priority -> preemption priority occurred but not disarmed when a transition from preemption priority -> cooperative priority occurred
* * The timer was unnecessarily disarmed in the case of updating the priority of a non-running thread, this isn't as much a bug as it is just pointless
* Priority inheritance in `KProcess::MutexLock` is fundamentally broken as it performs UB with `waitThread` being accessed prior to being assigned
* When a thread sets its own priority using  `SvcSetThreadCoreMask` and its current core is no longer in the affinity mask, it wouldn't actually move to the new thread until the next time the thread is load balanced
2021-03-05 14:55:34 +05:30
PixelyIon
fe5061a8e0 Address CR Comments (#132) + Change Core Migration API
This addresses all CR comments including more codebase-wide changes arising from certain review comments like proper usage of its/it's and consistent contraction of it is into it's. 

An overhaul was made to the presentation and formatting of `KThread.h` and `LoadBalance` works has been superseded by `GetOptimalCoreForThread` which can be used alongside `InsertThread` or `MigrateToCore`. It makes the API far more atomic and neater. This was a major point of contention for the design prior, it's simplified some code and potentially improved performance.
2021-03-05 14:55:34 +05:30
PixelyIon
0ea02f2d56 Fix Non-Cooperative Core Migration + Fix yieldWithCoreMigration + Improve Mutex Locking in ConditionalVariableWait
The case of a thread not being in the core queue during a non-cooperative core affinity change would break things as the thread was non-conditionally removed and inserted, this has been fixed by adding a check to see if the thread exists in the core's queue prior to migration. In addition, `yieldWithCoreMigration` was broken by the previous commit as the fallthrough was intentional and removing it cause core migration without a yield which led to breakage in certain circumstances. The mutex locking logic was also improved in `ConditionalVariableWait` to use atomics in a more effective manner with less atomic operations being performed overall.
2021-03-05 14:55:34 +05:30
PixelyIon
20bdda6a63 Support Core Migration for Running External Thread
We did not support migration of threads which were running in a non-cooperative manner, this was partially due to the dependence on per-core conditional variables rather than per-thread which made this harder to do programmatically. This has been fixed by moving to per-thread cvars and therefore the limitation can be removed, this feature is used by Unity games.
2021-03-05 14:55:34 +05:30
◱ PixelyIon
7e7a792dc5 Use per-thread scheduler condvars and clean up AS filters
Per-thread condvars previously caused issues due to an audren bug, now
that's fixed they have a clear performance benefit.
2021-03-05 14:55:34 +05:30
◱ PixelyIon
1f48fdd4a5 Fix Thread Insertion Optimization + Revert Per-Thread Scheduler Conditions 2021-03-05 14:55:34 +05:30
◱ PixelyIon
d5d133372f Fix Clean Exiting + Optimize Core Queues + Optimize Thread Insertion + Implement HID SendVibrationValue 2021-03-05 14:55:34 +05:30
◱ PixelyIon
98b1fd9056 Optimize Scheduler/IPC/HID + Fix Various Bugs
* Optimize Scheduler With Per-Thread Scheduler Conditions
* Optimize IPC by yielding
* Optimize HID Vibration
* Fix Priority Inheritance
* Fix `KThread` Start/Kill/Signal Races 
* Fix `YieldPending` Races in `StartThread` & `SvcHandler` 
* Fix POSIX Time -> NN CalendarTime Conversion 
* Fix HID `TouchScreen`/`NPad` Activation
2021-03-05 14:55:34 +05:30
◱ PixelyIon
14dbb5305a Fix Priority Queue + Cooperative Yielding + Conditional Variable Timeouts 2021-03-05 14:55:34 +05:30
◱ PixelyIon
33bbfb9fb7 Implement Conditional Variables 2021-03-05 14:55:34 +05:30
◱ PixelyIon
7079f11add Implement PI-Mutexes + Optimize InsertThread 2021-03-05 14:55:34 +05:30
◱ PixelyIon
7ba7cd2394 Support Priority & Affinity Mask Changes 2021-03-05 14:55:34 +05:30
◱ PixelyIon
f41bcd1e22 Implement Preemptive Scheduling 2021-03-05 14:55:34 +05:30
◱ PixelyIon
cf000f5750 Implement Cooperative Scheduling With Load Balancing 2021-03-05 14:55:34 +05:30