summaryrefslogtreecommitdiff
path: root/src/core/hle/kernel
diff options
context:
space:
mode:
authorGravatar bunnei2014-07-08 18:54:12 -0400
committerGravatar bunnei2014-07-08 18:54:12 -0400
commit584f7aced5360654d3f86dd14beb87a637b802f1 (patch)
treea4e8761b1b55ca53941db9f2ce8c04f224226238 /src/core/hle/kernel
parentMerge pull request #28 from bunnei/shared-memory (diff)
parentKernel: Added preliminary support for address arbiters. (diff)
downloadyuzu-584f7aced5360654d3f86dd14beb87a637b802f1.tar.gz
yuzu-584f7aced5360654d3f86dd14beb87a637b802f1.tar.xz
yuzu-584f7aced5360654d3f86dd14beb87a637b802f1.zip
Merge pull request #29 from bunnei/address-arbiters
Adds address arbiters to kernel HLE
Diffstat (limited to 'src/core/hle/kernel')
-rw-r--r--src/core/hle/kernel/address_arbiter.cpp87
-rw-r--r--src/core/hle/kernel/address_arbiter.h36
-rw-r--r--src/core/hle/kernel/kernel.h2
-rw-r--r--src/core/hle/kernel/thread.cpp37
-rw-r--r--src/core/hle/kernel/thread.h7
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 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2
3// Refer to the license.txt file included.
4
5#include "common/common_types.h"
6
7#include "core/mem_map.h"
8
9#include "core/hle/hle.h"
10#include "core/hle/kernel/address_arbiter.h"
11#include "core/hle/kernel/thread.h"
12
13////////////////////////////////////////////////////////////////////////////////////////////////////
14// Kernel namespace
15
16namespace Kernel {
17
18class AddressArbiter : public Object {
19public:
20 const char* GetTypeName() const { return "Arbiter"; }
21 const char* GetName() const { return name.c_str(); }
22
23 static Kernel::HandleType GetStaticHandleType() { return HandleType::AddressArbiter; }
24 Kernel::HandleType GetHandleType() const { return HandleType::AddressArbiter; }
25
26 std::string name; ///< Name of address arbiter object (optional)
27
28 /**
29 * Wait for kernel object to synchronize
30 * @param wait Boolean wait set if current thread should wait as a result of sync operation
31 * @return Result of operation, 0 on success, otherwise error code
32 */
33 Result WaitSynchronization(bool* wait) {
34 // TODO(bunnei): ImplementMe
35 ERROR_LOG(OSHLE, "(UNIMPLEMENTED)");
36 return 0;
37 }
38};
39
40////////////////////////////////////////////////////////////////////////////////////////////////////
41
42/// Arbitrate an address
43Result ArbitrateAddress(Handle handle, ArbitrationType type, u32 address, s32 value) {
44 switch (type) {
45
46 // Signal thread(s) waiting for arbitrate address...
47 case ArbitrationType::Signal:
48 // Negative value means resume all threads
49 if (value < 0) {
50 ArbitrateAllThreads(handle, address);
51 } else {
52 // Resume first N threads
53 for(int i = 0; i < value; i++)
54 ArbitrateHighestPriorityThread(handle, address);
55 }
56 HLE::Reschedule(__func__);
57
58 // Wait current thread (acquire the arbiter)...
59 case ArbitrationType::WaitIfLessThan:
60 if ((s32)Memory::Read32(address) <= value) {
61 Kernel::WaitCurrentThread(WAITTYPE_ARB, handle);
62 HLE::Reschedule(__func__);
63 }
64
65 default:
66 ERROR_LOG(KERNEL, "unknown type=%d", type);
67 return -1;
68 }
69 return 0;
70}
71
72/// Create an address arbiter
73AddressArbiter* CreateAddressArbiter(Handle& handle, const std::string& name) {
74 AddressArbiter* address_arbiter = new AddressArbiter;
75 handle = Kernel::g_object_pool.Create(address_arbiter);
76 address_arbiter->name = name;
77 return address_arbiter;
78}
79
80/// Create an address arbiter
81Handle CreateAddressArbiter(const std::string& name) {
82 Handle handle;
83 CreateAddressArbiter(handle, name);
84 return handle;
85}
86
87} // 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 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "common/common_types.h"
8
9#include "core/hle/kernel/kernel.h"
10
11// Address arbiters are an underlying kernel synchronization object that can be created/used via
12// supervisor calls (SVCs). They function as sort of a global lock. Typically, games/other CTR
13// applications use them as an underlying mechanism to implement thread-safe barriers, events, and
14// semphores.
15
16////////////////////////////////////////////////////////////////////////////////////////////////////
17// Kernel namespace
18
19namespace Kernel {
20
21/// Address arbitration types
22enum class ArbitrationType : u32 {
23 Signal,
24 WaitIfLessThan,
25 DecrementAndWaitIfLessThan,
26 WaitIfLessThanWithTimeout,
27 DecrementAndWaitIfLessThanWithTimeout,
28};
29
30/// Arbitrate an address
31Result ArbitrateAddress(Handle handle, ArbitrationType type, u32 address, s32 value);
32
33/// Create an address arbiter
34Handle CreateAddressArbiter(const std::string& name = "Unknown");
35
36} // 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 {
26 Redirection = 6, 26 Redirection = 6,
27 Thread = 7, 27 Thread = 7,
28 Process = 8, 28 Process = 8,
29 Arbiter = 9, 29 AddressArbiter = 9,
30 File = 10, 30 File = 10,
31 Semaphore = 11, 31 Semaphore = 11,
32 Archive = 12, 32 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) {
188 } 188 }
189} 189}
190 190
191/// Arbitrate the highest priority thread that is waiting
192Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address) {
193 Handle highest_priority_thread = 0;
194 s32 priority = THREADPRIO_LOWEST;
195
196 // Iterate through threads, find highest priority thread that is waiting to be arbitrated...
197 for (const auto& handle : g_thread_queue) {
198
199 // TODO(bunnei): Verify arbiter address...
200 if (!VerifyWait(handle, WAITTYPE_ARB, arbiter))
201 continue;
202
203 Thread* thread = g_object_pool.GetFast<Thread>(handle);
204 if(thread->current_priority <= priority) {
205 highest_priority_thread = handle;
206 priority = thread->current_priority;
207 }
208 }
209 // If a thread was arbitrated, resume it
210 if (0 != highest_priority_thread)
211 ResumeThreadFromWait(highest_priority_thread);
212
213 return highest_priority_thread;
214}
215
216/// Arbitrate all threads currently waiting
217void ArbitrateAllThreads(u32 arbiter, u32 address) {
218
219 // Iterate through threads, find highest priority thread that is waiting to be arbitrated...
220 for (const auto& handle : g_thread_queue) {
221
222 // TODO(bunnei): Verify arbiter address...
223 if (VerifyWait(handle, WAITTYPE_ARB, arbiter))
224 ResumeThreadFromWait(handle);
225 }
226}
227
191/// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields) 228/// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields)
192void CallThread(Thread* t) { 229void CallThread(Thread* t) {
193 // Stop waiting 230 // 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 {
39 WAITTYPE_VBLANK, 39 WAITTYPE_VBLANK,
40 WAITTYPE_MUTEX, 40 WAITTYPE_MUTEX,
41 WAITTYPE_SYNCH, 41 WAITTYPE_SYNCH,
42 WAITTYPE_ARB,
42}; 43};
43 44
44namespace Kernel { 45namespace Kernel {
@@ -59,6 +60,12 @@ void StopThread(Handle thread, const char* reason);
59/// Resumes a thread from waiting by marking it as "ready" 60/// Resumes a thread from waiting by marking it as "ready"
60void ResumeThreadFromWait(Handle handle); 61void ResumeThreadFromWait(Handle handle);
61 62
63/// Arbitrate the highest priority thread that is waiting
64Handle ArbitrateHighestPriorityThread(u32 arbiter, u32 address);
65
66/// Arbitrate all threads currently waiting...
67void ArbitrateAllThreads(u32 arbiter, u32 address);
68
62/// Gets the current thread handle 69/// Gets the current thread handle
63Handle GetCurrentThreadHandle(); 70Handle GetCurrentThreadHandle();
64 71