diff options
author | bunnei <bunneidev@gmail.com> | 2014-07-09 00:54:12 +0200 |
---|---|---|
committer | bunnei <bunneidev@gmail.com> | 2014-07-09 00:54:12 +0200 |
commit | 584f7aced5360654d3f86dd14beb87a637b802f1 (patch) | |
tree | a4e8761b1b55ca53941db9f2ce8c04f224226238 /src/core/hle/kernel | |
parent | Merge pull request #28 from bunnei/shared-memory (diff) | |
parent | Kernel: Added preliminary support for address arbiters. (diff) | |
download | yuzu-584f7aced5360654d3f86dd14beb87a637b802f1.tar yuzu-584f7aced5360654d3f86dd14beb87a637b802f1.tar.gz yuzu-584f7aced5360654d3f86dd14beb87a637b802f1.tar.bz2 yuzu-584f7aced5360654d3f86dd14beb87a637b802f1.tar.lz yuzu-584f7aced5360654d3f86dd14beb87a637b802f1.tar.xz yuzu-584f7aced5360654d3f86dd14beb87a637b802f1.tar.zst yuzu-584f7aced5360654d3f86dd14beb87a637b802f1.zip |
Diffstat (limited to 'src/core/hle/kernel')
-rw-r--r-- | src/core/hle/kernel/address_arbiter.cpp | 87 | ||||
-rw-r--r-- | src/core/hle/kernel/address_arbiter.h | 36 | ||||
-rw-r--r-- | src/core/hle/kernel/kernel.h | 2 | ||||
-rw-r--r-- | src/core/hle/kernel/thread.cpp | 37 | ||||
-rw-r--r-- | src/core/hle/kernel/thread.h | 7 |
5 files changed, 168 insertions, 1 deletions
diff --git a/src/core/hle/kernel/address_arbiter.cpp b/src/core/hle/kernel/address_arbiter.cpp new file mode 100644 index 000000000..61717bbe4 --- /dev/null +++ b/src/core/hle/kernel/address_arbiter.cpp @@ -0,0 +1,87 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include "common/common_types.h" + +#include "core/mem_map.h" + +#include "core/hle/hle.h" +#include "core/hle/kernel/address_arbiter.h" +#include "core/hle/kernel/thread.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Kernel namespace + +namespace Kernel { + +class AddressArbiter : public Object { +public: + const char* GetTypeName() const { return "Arbiter"; } + const char* GetName() const { return name.c_str(); } + + static Kernel::HandleType GetStaticHandleType() { return HandleType::AddressArbiter; } + Kernel::HandleType GetHandleType() const { return HandleType::AddressArbiter; } + + std::string name; ///< Name of address arbiter object (optional) + + /** + * Wait for kernel object to synchronize + * @param wait Boolean wait set if current thread should wait as a result of sync operation + * @return Result of operation, 0 on success, otherwise error code + */ + Result WaitSynchronization(bool* wait) { + // TODO(bunnei): ImplementMe + ERROR_LOG(OSHLE, "(UNIMPLEMENTED)"); + return 0; + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/// Arbitrate an address +Result ArbitrateAddress(Handle handle, ArbitrationType type, u32 address, s32 value) { + switch (type) { + + // Signal thread(s) waiting for arbitrate address... + case ArbitrationType::Signal: + // Negative value means resume all threads + if (value < 0) { + ArbitrateAllThreads(handle, address); + } else { + // Resume first N threads + for(int i = 0; i < value; i++) + ArbitrateHighestPriorityThread(handle, address); + } + HLE::Reschedule(__func__); + + // Wait current thread (acquire the arbiter)... + case ArbitrationType::WaitIfLessThan: + if ((s32)Memory::Read32(address) <= value) { + Kernel::WaitCurrentThread(WAITTYPE_ARB, handle); + HLE::Reschedule(__func__); + } + + default: + ERROR_LOG(KERNEL, "unknown type=%d", type); + return -1; + } + return 0; +} + +/// Create an address arbiter +AddressArbiter* CreateAddressArbiter(Handle& handle, const std::string& name) { + AddressArbiter* address_arbiter = new AddressArbiter; + handle = Kernel::g_object_pool.Create(address_arbiter); + address_arbiter->name = name; + return address_arbiter; +} + +/// Create an address arbiter +Handle CreateAddressArbiter(const std::string& name) { + Handle handle; + CreateAddressArbiter(handle, name); + return handle; +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/address_arbiter.h b/src/core/hle/kernel/address_arbiter.h new file mode 100644 index 000000000..a483fe466 --- /dev/null +++ b/src/core/hle/kernel/address_arbiter.h @@ -0,0 +1,36 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" + +#include "core/hle/kernel/kernel.h" + +// Address arbiters are an underlying kernel synchronization object that can be created/used via +// supervisor calls (SVCs). They function as sort of a global lock. Typically, games/other CTR +// applications use them as an underlying mechanism to implement thread-safe barriers, events, and +// semphores. + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Kernel namespace + +namespace Kernel { + +/// Address arbitration types +enum class ArbitrationType : u32 { + Signal, + WaitIfLessThan, + DecrementAndWaitIfLessThan, + WaitIfLessThanWithTimeout, + DecrementAndWaitIfLessThanWithTimeout, +}; + +/// Arbitrate an address +Result ArbitrateAddress(Handle handle, ArbitrationType type, u32 address, s32 value); + +/// Create an address arbiter +Handle CreateAddressArbiter(const std::string& name = "Unknown"); + +} // namespace FileSys diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h index 69f4ddd37..d9afcdd25 100644 --- a/src/core/hle/kernel/kernel.h +++ b/src/core/hle/kernel/kernel.h @@ -26,7 +26,7 @@ enum class HandleType : u32 { Redirection = 6, Thread = 7, Process = 8, - Arbiter = 9, + AddressArbiter = 9, File = 10, Semaphore = 11, Archive = 12, diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index ab5a5559e..86bbf29d0 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -188,6 +188,43 @@ void ChangeThreadState(Thread* t, ThreadStatus new_status) { } } +/// Arbitrate the highest priority thread that is waiting +Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address) { + Handle highest_priority_thread = 0; + s32 priority = THREADPRIO_LOWEST; + + // Iterate through threads, find highest priority thread that is waiting to be arbitrated... + for (const auto& handle : g_thread_queue) { + + // TODO(bunnei): Verify arbiter address... + if (!VerifyWait(handle, WAITTYPE_ARB, arbiter)) + continue; + + Thread* thread = g_object_pool.GetFast<Thread>(handle); + if(thread->current_priority <= priority) { + highest_priority_thread = handle; + priority = thread->current_priority; + } + } + // If a thread was arbitrated, resume it + if (0 != highest_priority_thread) + ResumeThreadFromWait(highest_priority_thread); + + return highest_priority_thread; +} + +/// Arbitrate all threads currently waiting +void ArbitrateAllThreads(u32 arbiter, u32 address) { + + // Iterate through threads, find highest priority thread that is waiting to be arbitrated... + for (const auto& handle : g_thread_queue) { + + // TODO(bunnei): Verify arbiter address... + if (VerifyWait(handle, WAITTYPE_ARB, arbiter)) + ResumeThreadFromWait(handle); + } +} + /// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields) void CallThread(Thread* t) { // Stop waiting diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 04914ba90..f2bfdfa1a 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -39,6 +39,7 @@ enum WaitType { WAITTYPE_VBLANK, WAITTYPE_MUTEX, WAITTYPE_SYNCH, + WAITTYPE_ARB, }; namespace Kernel { @@ -59,6 +60,12 @@ void StopThread(Handle thread, const char* reason); /// Resumes a thread from waiting by marking it as "ready" void ResumeThreadFromWait(Handle handle); +/// Arbitrate the highest priority thread that is waiting +Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address); + +/// Arbitrate all threads currently waiting... +void ArbitrateAllThreads(u32 arbiter, u32 address); + /// Gets the current thread handle Handle GetCurrentThreadHandle(); |