address_arbiter.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <vector>
  6. #include "common/assert.h"
  7. #include "common/common_types.h"
  8. #include "core/arm/exclusive_monitor.h"
  9. #include "core/core.h"
  10. #include "core/hle/kernel/address_arbiter.h"
  11. #include "core/hle/kernel/errors.h"
  12. #include "core/hle/kernel/handle_table.h"
  13. #include "core/hle/kernel/k_scheduler.h"
  14. #include "core/hle/kernel/kernel.h"
  15. #include "core/hle/kernel/thread.h"
  16. #include "core/hle/kernel/time_manager.h"
  17. #include "core/hle/result.h"
  18. #include "core/memory.h"
  19. namespace Kernel {
  20. // Wake up num_to_wake (or all) threads in a vector.
  21. void AddressArbiter::WakeThreads(const std::vector<std::shared_ptr<Thread>>& waiting_threads,
  22. s32 num_to_wake) {
  23. // Only process up to 'target' threads, unless 'target' is <= 0, in which case process
  24. // them all.
  25. std::size_t last = waiting_threads.size();
  26. if (num_to_wake > 0) {
  27. last = std::min(last, static_cast<std::size_t>(num_to_wake));
  28. }
  29. // Signal the waiting threads.
  30. for (std::size_t i = 0; i < last; i++) {
  31. waiting_threads[i]->SetSynchronizationResults(nullptr, RESULT_SUCCESS);
  32. RemoveThread(waiting_threads[i]);
  33. waiting_threads[i]->WaitForArbitration(false);
  34. waiting_threads[i]->ResumeFromWait();
  35. }
  36. }
  37. AddressArbiter::AddressArbiter(Core::System& system) : system{system} {}
  38. AddressArbiter::~AddressArbiter() = default;
  39. ResultCode AddressArbiter::SignalToAddress(VAddr address, SignalType type, s32 value,
  40. s32 num_to_wake) {
  41. switch (type) {
  42. case SignalType::Signal:
  43. return SignalToAddressOnly(address, num_to_wake);
  44. case SignalType::IncrementAndSignalIfEqual:
  45. return IncrementAndSignalToAddressIfEqual(address, value, num_to_wake);
  46. case SignalType::ModifyByWaitingCountAndSignalIfEqual:
  47. return ModifyByWaitingCountAndSignalToAddressIfEqual(address, value, num_to_wake);
  48. default:
  49. return ERR_INVALID_ENUM_VALUE;
  50. }
  51. }
  52. ResultCode AddressArbiter::SignalToAddressOnly(VAddr address, s32 num_to_wake) {
  53. SchedulerLock lock(system.Kernel());
  54. const std::vector<std::shared_ptr<Thread>> waiting_threads =
  55. GetThreadsWaitingOnAddress(address);
  56. WakeThreads(waiting_threads, num_to_wake);
  57. return RESULT_SUCCESS;
  58. }
  59. ResultCode AddressArbiter::IncrementAndSignalToAddressIfEqual(VAddr address, s32 value,
  60. s32 num_to_wake) {
  61. SchedulerLock lock(system.Kernel());
  62. auto& memory = system.Memory();
  63. // Ensure that we can write to the address.
  64. if (!memory.IsValidVirtualAddress(address)) {
  65. return ERR_INVALID_ADDRESS_STATE;
  66. }
  67. const std::size_t current_core = system.CurrentCoreIndex();
  68. auto& monitor = system.Monitor();
  69. u32 current_value;
  70. do {
  71. current_value = monitor.ExclusiveRead32(current_core, address);
  72. if (current_value != static_cast<u32>(value)) {
  73. return ERR_INVALID_STATE;
  74. }
  75. current_value++;
  76. } while (!monitor.ExclusiveWrite32(current_core, address, current_value));
  77. return SignalToAddressOnly(address, num_to_wake);
  78. }
  79. ResultCode AddressArbiter::ModifyByWaitingCountAndSignalToAddressIfEqual(VAddr address, s32 value,
  80. s32 num_to_wake) {
  81. SchedulerLock lock(system.Kernel());
  82. auto& memory = system.Memory();
  83. // Ensure that we can write to the address.
  84. if (!memory.IsValidVirtualAddress(address)) {
  85. return ERR_INVALID_ADDRESS_STATE;
  86. }
  87. // Get threads waiting on the address.
  88. const std::vector<std::shared_ptr<Thread>> waiting_threads =
  89. GetThreadsWaitingOnAddress(address);
  90. const std::size_t current_core = system.CurrentCoreIndex();
  91. auto& monitor = system.Monitor();
  92. s32 updated_value;
  93. do {
  94. updated_value = monitor.ExclusiveRead32(current_core, address);
  95. if (updated_value != value) {
  96. return ERR_INVALID_STATE;
  97. }
  98. // Determine the modified value depending on the waiting count.
  99. if (num_to_wake <= 0) {
  100. if (waiting_threads.empty()) {
  101. updated_value = value + 1;
  102. } else {
  103. updated_value = value - 1;
  104. }
  105. } else {
  106. if (waiting_threads.empty()) {
  107. updated_value = value + 1;
  108. } else if (waiting_threads.size() <= static_cast<u32>(num_to_wake)) {
  109. updated_value = value - 1;
  110. } else {
  111. updated_value = value;
  112. }
  113. }
  114. } while (!monitor.ExclusiveWrite32(current_core, address, updated_value));
  115. WakeThreads(waiting_threads, num_to_wake);
  116. return RESULT_SUCCESS;
  117. }
  118. ResultCode AddressArbiter::WaitForAddress(VAddr address, ArbitrationType type, s32 value,
  119. s64 timeout_ns) {
  120. switch (type) {
  121. case ArbitrationType::WaitIfLessThan:
  122. return WaitForAddressIfLessThan(address, value, timeout_ns, false);
  123. case ArbitrationType::DecrementAndWaitIfLessThan:
  124. return WaitForAddressIfLessThan(address, value, timeout_ns, true);
  125. case ArbitrationType::WaitIfEqual:
  126. return WaitForAddressIfEqual(address, value, timeout_ns);
  127. default:
  128. return ERR_INVALID_ENUM_VALUE;
  129. }
  130. }
  131. ResultCode AddressArbiter::WaitForAddressIfLessThan(VAddr address, s32 value, s64 timeout,
  132. bool should_decrement) {
  133. auto& memory = system.Memory();
  134. auto& kernel = system.Kernel();
  135. Thread* current_thread = kernel.CurrentScheduler()->GetCurrentThread();
  136. Handle event_handle = InvalidHandle;
  137. {
  138. SchedulerLockAndSleep lock(kernel, event_handle, current_thread, timeout);
  139. if (current_thread->IsPendingTermination()) {
  140. lock.CancelSleep();
  141. return ERR_THREAD_TERMINATING;
  142. }
  143. // Ensure that we can read the address.
  144. if (!memory.IsValidVirtualAddress(address)) {
  145. lock.CancelSleep();
  146. return ERR_INVALID_ADDRESS_STATE;
  147. }
  148. s32 current_value = static_cast<s32>(memory.Read32(address));
  149. if (current_value >= value) {
  150. lock.CancelSleep();
  151. return ERR_INVALID_STATE;
  152. }
  153. current_thread->SetSynchronizationResults(nullptr, RESULT_TIMEOUT);
  154. s32 decrement_value;
  155. const std::size_t current_core = system.CurrentCoreIndex();
  156. auto& monitor = system.Monitor();
  157. do {
  158. current_value = static_cast<s32>(monitor.ExclusiveRead32(current_core, address));
  159. if (should_decrement) {
  160. decrement_value = current_value - 1;
  161. } else {
  162. decrement_value = current_value;
  163. }
  164. } while (
  165. !monitor.ExclusiveWrite32(current_core, address, static_cast<u32>(decrement_value)));
  166. // Short-circuit without rescheduling, if timeout is zero.
  167. if (timeout == 0) {
  168. lock.CancelSleep();
  169. return RESULT_TIMEOUT;
  170. }
  171. current_thread->SetArbiterWaitAddress(address);
  172. InsertThread(SharedFrom(current_thread));
  173. current_thread->SetStatus(ThreadStatus::WaitArb);
  174. current_thread->WaitForArbitration(true);
  175. }
  176. if (event_handle != InvalidHandle) {
  177. auto& time_manager = kernel.TimeManager();
  178. time_manager.UnscheduleTimeEvent(event_handle);
  179. }
  180. {
  181. SchedulerLock lock(kernel);
  182. if (current_thread->IsWaitingForArbitration()) {
  183. RemoveThread(SharedFrom(current_thread));
  184. current_thread->WaitForArbitration(false);
  185. }
  186. }
  187. return current_thread->GetSignalingResult();
  188. }
  189. ResultCode AddressArbiter::WaitForAddressIfEqual(VAddr address, s32 value, s64 timeout) {
  190. auto& memory = system.Memory();
  191. auto& kernel = system.Kernel();
  192. Thread* current_thread = kernel.CurrentScheduler()->GetCurrentThread();
  193. Handle event_handle = InvalidHandle;
  194. {
  195. SchedulerLockAndSleep lock(kernel, event_handle, current_thread, timeout);
  196. if (current_thread->IsPendingTermination()) {
  197. lock.CancelSleep();
  198. return ERR_THREAD_TERMINATING;
  199. }
  200. // Ensure that we can read the address.
  201. if (!memory.IsValidVirtualAddress(address)) {
  202. lock.CancelSleep();
  203. return ERR_INVALID_ADDRESS_STATE;
  204. }
  205. s32 current_value = static_cast<s32>(memory.Read32(address));
  206. if (current_value != value) {
  207. lock.CancelSleep();
  208. return ERR_INVALID_STATE;
  209. }
  210. // Short-circuit without rescheduling, if timeout is zero.
  211. if (timeout == 0) {
  212. lock.CancelSleep();
  213. return RESULT_TIMEOUT;
  214. }
  215. current_thread->SetSynchronizationResults(nullptr, RESULT_TIMEOUT);
  216. current_thread->SetArbiterWaitAddress(address);
  217. InsertThread(SharedFrom(current_thread));
  218. current_thread->SetStatus(ThreadStatus::WaitArb);
  219. current_thread->WaitForArbitration(true);
  220. }
  221. if (event_handle != InvalidHandle) {
  222. auto& time_manager = kernel.TimeManager();
  223. time_manager.UnscheduleTimeEvent(event_handle);
  224. }
  225. {
  226. SchedulerLock lock(kernel);
  227. if (current_thread->IsWaitingForArbitration()) {
  228. RemoveThread(SharedFrom(current_thread));
  229. current_thread->WaitForArbitration(false);
  230. }
  231. }
  232. return current_thread->GetSignalingResult();
  233. }
  234. void AddressArbiter::InsertThread(std::shared_ptr<Thread> thread) {
  235. const VAddr arb_addr = thread->GetArbiterWaitAddress();
  236. std::list<std::shared_ptr<Thread>>& thread_list = arb_threads[arb_addr];
  237. const auto iter =
  238. std::find_if(thread_list.cbegin(), thread_list.cend(), [&thread](const auto& entry) {
  239. return entry->GetPriority() >= thread->GetPriority();
  240. });
  241. if (iter == thread_list.cend()) {
  242. thread_list.push_back(std::move(thread));
  243. } else {
  244. thread_list.insert(iter, std::move(thread));
  245. }
  246. }
  247. void AddressArbiter::RemoveThread(std::shared_ptr<Thread> thread) {
  248. const VAddr arb_addr = thread->GetArbiterWaitAddress();
  249. std::list<std::shared_ptr<Thread>>& thread_list = arb_threads[arb_addr];
  250. const auto iter = std::find_if(thread_list.cbegin(), thread_list.cend(),
  251. [&thread](const auto& entry) { return thread == entry; });
  252. if (iter != thread_list.cend()) {
  253. thread_list.erase(iter);
  254. }
  255. }
  256. std::vector<std::shared_ptr<Thread>> AddressArbiter::GetThreadsWaitingOnAddress(
  257. VAddr address) const {
  258. const auto iter = arb_threads.find(address);
  259. if (iter == arb_threads.cend()) {
  260. return {};
  261. }
  262. const std::list<std::shared_ptr<Thread>>& thread_list = iter->second;
  263. return {thread_list.cbegin(), thread_list.cend()};
  264. }
  265. } // namespace Kernel