svc.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <map>
  5. #include "common/string_util.h"
  6. #include "common/symbols.h"
  7. #include "core/arm/arm_interface.h"
  8. #include "core/mem_map.h"
  9. #include "core/hle/kernel/address_arbiter.h"
  10. #include "core/hle/kernel/event.h"
  11. #include "core/hle/kernel/mutex.h"
  12. #include "core/hle/kernel/semaphore.h"
  13. #include "core/hle/kernel/shared_memory.h"
  14. #include "core/hle/kernel/thread.h"
  15. #include "core/hle/kernel/timer.h"
  16. #include "core/hle/function_wrappers.h"
  17. #include "core/hle/result.h"
  18. #include "core/hle/service/service.h"
  19. ////////////////////////////////////////////////////////////////////////////////////////////////////
  20. // Namespace SVC
  21. using Kernel::SharedPtr;
  22. using Kernel::ERR_INVALID_HANDLE;
  23. namespace SVC {
  24. const ResultCode ERR_NOT_FOUND(ErrorDescription::NotFound, ErrorModule::Kernel,
  25. ErrorSummary::NotFound, ErrorLevel::Permanent); // 0xD88007FA
  26. const ResultCode ERR_PORT_NAME_TOO_LONG(ErrorDescription(30), ErrorModule::OS,
  27. ErrorSummary::InvalidArgument, ErrorLevel::Usage); // 0xE0E0181E
  28. /// An invalid result code that is meant to be overwritten when a thread resumes from waiting
  29. const ResultCode RESULT_INVALID(0xDEADC0DE);
  30. enum ControlMemoryOperation {
  31. MEMORY_OPERATION_HEAP = 0x00000003,
  32. MEMORY_OPERATION_GSP_HEAP = 0x00010003,
  33. };
  34. /// Map application or GSP heap memory
  35. static ResultCode ControlMemory(u32* out_addr, u32 operation, u32 addr0, u32 addr1, u32 size, u32 permissions) {
  36. LOG_TRACE(Kernel_SVC,"called operation=0x%08X, addr0=0x%08X, addr1=0x%08X, size=%08X, permissions=0x%08X",
  37. operation, addr0, addr1, size, permissions);
  38. switch (operation) {
  39. // Map normal heap memory
  40. case MEMORY_OPERATION_HEAP:
  41. *out_addr = Memory::MapBlock_Heap(size, operation, permissions);
  42. break;
  43. // Map GSP heap memory
  44. case MEMORY_OPERATION_GSP_HEAP:
  45. *out_addr = Memory::MapBlock_HeapLinear(size, operation, permissions);
  46. break;
  47. // Unknown ControlMemory operation
  48. default:
  49. LOG_ERROR(Kernel_SVC, "unknown operation=0x%08X", operation);
  50. }
  51. return RESULT_SUCCESS;
  52. }
  53. /// Maps a memory block to specified address
  54. static ResultCode MapMemoryBlock(Handle handle, u32 addr, u32 permissions, u32 other_permissions) {
  55. using Kernel::SharedMemory;
  56. using Kernel::MemoryPermission;
  57. LOG_TRACE(Kernel_SVC, "called memblock=0x%08X, addr=0x%08X, mypermissions=0x%08X, otherpermission=%d",
  58. handle, addr, permissions, other_permissions);
  59. SharedPtr<SharedMemory> shared_memory = Kernel::g_handle_table.Get<SharedMemory>(handle);
  60. if (shared_memory == nullptr)
  61. return ERR_INVALID_HANDLE;
  62. MemoryPermission permissions_type = static_cast<MemoryPermission>(permissions);
  63. switch (permissions_type) {
  64. case MemoryPermission::Read:
  65. case MemoryPermission::Write:
  66. case MemoryPermission::ReadWrite:
  67. case MemoryPermission::Execute:
  68. case MemoryPermission::ReadExecute:
  69. case MemoryPermission::WriteExecute:
  70. case MemoryPermission::ReadWriteExecute:
  71. case MemoryPermission::DontCare:
  72. shared_memory->Map(addr, permissions_type,
  73. static_cast<MemoryPermission>(other_permissions));
  74. break;
  75. default:
  76. LOG_ERROR(Kernel_SVC, "unknown permissions=0x%08X", permissions);
  77. }
  78. return RESULT_SUCCESS;
  79. }
  80. /// Connect to an OS service given the port name, returns the handle to the port to out
  81. static ResultCode ConnectToPort(Handle* out_handle, const char* port_name) {
  82. if (port_name == nullptr)
  83. return ERR_NOT_FOUND;
  84. if (std::strlen(port_name) > 11)
  85. return ERR_PORT_NAME_TOO_LONG;
  86. LOG_TRACE(Kernel_SVC, "called port_name=%s", port_name);
  87. auto it = Service::g_kernel_named_ports.find(port_name);
  88. if (it == Service::g_kernel_named_ports.end()) {
  89. LOG_WARNING(Kernel_SVC, "tried to connect to unknown port: %s", port_name);
  90. return ERR_NOT_FOUND;
  91. }
  92. CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(it->second));
  93. return RESULT_SUCCESS;
  94. }
  95. /// Synchronize to an OS service
  96. static ResultCode SendSyncRequest(Handle handle) {
  97. SharedPtr<Kernel::Session> session = Kernel::g_handle_table.Get<Kernel::Session>(handle);
  98. if (session == nullptr) {
  99. return ERR_INVALID_HANDLE;
  100. }
  101. LOG_TRACE(Kernel_SVC, "called handle=0x%08X(%s)", handle, session->GetName().c_str());
  102. return session->SyncRequest().Code();
  103. }
  104. /// Close a handle
  105. static ResultCode CloseHandle(Handle handle) {
  106. LOG_TRACE(Kernel_SVC, "Closing handle 0x%08X", handle);
  107. return Kernel::g_handle_table.Close(handle);
  108. }
  109. /// Wait for a handle to synchronize, timeout after the specified nanoseconds
  110. static ResultCode WaitSynchronization1(Handle handle, s64 nano_seconds) {
  111. auto object = Kernel::g_handle_table.GetWaitObject(handle);
  112. if (object == nullptr)
  113. return ERR_INVALID_HANDLE;
  114. LOG_TRACE(Kernel_SVC, "called handle=0x%08X(%s:%s), nanoseconds=%lld", handle,
  115. object->GetTypeName().c_str(), object->GetName().c_str(), nano_seconds);
  116. HLE::Reschedule(__func__);
  117. // Check for next thread to schedule
  118. if (object->ShouldWait()) {
  119. object->AddWaitingThread(Kernel::GetCurrentThread());
  120. Kernel::WaitCurrentThread_WaitSynchronization({ object }, false, false);
  121. // Create an event to wake the thread up after the specified nanosecond delay has passed
  122. Kernel::GetCurrentThread()->WakeAfterDelay(nano_seconds);
  123. // NOTE: output of this SVC will be set later depending on how the thread resumes
  124. return RESULT_INVALID;
  125. }
  126. object->Acquire();
  127. return RESULT_SUCCESS;
  128. }
  129. /// Wait for the given handles to synchronize, timeout after the specified nanoseconds
  130. static ResultCode WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count, bool wait_all, s64 nano_seconds) {
  131. bool wait_thread = !wait_all;
  132. int handle_index = 0;
  133. // Check if 'handles' is invalid
  134. if (handles == nullptr)
  135. return ResultCode(ErrorDescription::InvalidPointer, ErrorModule::Kernel, ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
  136. // NOTE: on real hardware, there is no nullptr check for 'out' (tested with firmware 4.4). If
  137. // this happens, the running application will crash.
  138. ASSERT_MSG(out != nullptr, "invalid output pointer specified!");
  139. // Check if 'handle_count' is invalid
  140. if (handle_count < 0)
  141. return ResultCode(ErrorDescription::OutOfRange, ErrorModule::OS, ErrorSummary::InvalidArgument, ErrorLevel::Usage);
  142. // If 'handle_count' is non-zero, iterate through each handle and wait the current thread if
  143. // necessary
  144. if (handle_count != 0) {
  145. bool selected = false; // True once an object has been selected
  146. for (int i = 0; i < handle_count; ++i) {
  147. auto object = Kernel::g_handle_table.GetWaitObject(handles[i]);
  148. if (object == nullptr)
  149. return ERR_INVALID_HANDLE;
  150. // Check if the current thread should wait on this object...
  151. if (object->ShouldWait()) {
  152. // Check we are waiting on all objects...
  153. if (wait_all)
  154. // Wait the thread
  155. wait_thread = true;
  156. } else {
  157. // Do not wait on this object, check if this object should be selected...
  158. if (!wait_all && !selected) {
  159. // Do not wait the thread
  160. wait_thread = false;
  161. handle_index = i;
  162. selected = true;
  163. }
  164. }
  165. }
  166. } else {
  167. // If no handles were passed in, put the thread to sleep only when 'wait_all' is false
  168. // NOTE: This should deadlock the current thread if no timeout was specified
  169. if (!wait_all) {
  170. wait_thread = true;
  171. }
  172. }
  173. HLE::Reschedule(__func__);
  174. // If thread should wait, then set its state to waiting and then reschedule...
  175. if (wait_thread) {
  176. // Actually wait the current thread on each object if we decided to wait...
  177. std::vector<SharedPtr<Kernel::WaitObject>> wait_objects;
  178. wait_objects.reserve(handle_count);
  179. for (int i = 0; i < handle_count; ++i) {
  180. auto object = Kernel::g_handle_table.GetWaitObject(handles[i]);
  181. object->AddWaitingThread(Kernel::GetCurrentThread());
  182. wait_objects.push_back(object);
  183. }
  184. Kernel::WaitCurrentThread_WaitSynchronization(std::move(wait_objects), true, wait_all);
  185. // Create an event to wake the thread up after the specified nanosecond delay has passed
  186. Kernel::GetCurrentThread()->WakeAfterDelay(nano_seconds);
  187. // NOTE: output of this SVC will be set later depending on how the thread resumes
  188. return RESULT_INVALID;
  189. }
  190. // Acquire objects if we did not wait...
  191. for (int i = 0; i < handle_count; ++i) {
  192. auto object = Kernel::g_handle_table.GetWaitObject(handles[i]);
  193. // Acquire the object if it is not waiting...
  194. if (!object->ShouldWait()) {
  195. object->Acquire();
  196. // If this was the first non-waiting object and 'wait_all' is false, don't acquire
  197. // any other objects
  198. if (!wait_all)
  199. break;
  200. }
  201. }
  202. // TODO(bunnei): If 'wait_all' is true, this is probably wrong. However, real hardware does
  203. // not seem to set it to any meaningful value.
  204. *out = wait_all ? 0 : handle_index;
  205. return RESULT_SUCCESS;
  206. }
  207. /// Create an address arbiter (to allocate access to shared resources)
  208. static ResultCode CreateAddressArbiter(Handle* out_handle) {
  209. using Kernel::AddressArbiter;
  210. SharedPtr<AddressArbiter> arbiter = AddressArbiter::Create();
  211. CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(std::move(arbiter)));
  212. LOG_TRACE(Kernel_SVC, "returned handle=0x%08X", *out_handle);
  213. return RESULT_SUCCESS;
  214. }
  215. /// Arbitrate address
  216. static ResultCode ArbitrateAddress(Handle handle, u32 address, u32 type, u32 value, s64 nanoseconds) {
  217. using Kernel::AddressArbiter;
  218. LOG_TRACE(Kernel_SVC, "called handle=0x%08X, address=0x%08X, type=0x%08X, value=0x%08X", handle,
  219. address, type, value);
  220. SharedPtr<AddressArbiter> arbiter = Kernel::g_handle_table.Get<AddressArbiter>(handle);
  221. if (arbiter == nullptr)
  222. return ERR_INVALID_HANDLE;
  223. return arbiter->ArbitrateAddress(static_cast<Kernel::ArbitrationType>(type),
  224. address, value, nanoseconds);
  225. }
  226. /// Used to output a message on a debug hardware unit - does nothing on a retail unit
  227. static void OutputDebugString(const char* string) {
  228. LOG_DEBUG(Debug_Emulated, "%s", string);
  229. }
  230. /// Get resource limit
  231. static ResultCode GetResourceLimit(Handle* resource_limit, Handle process) {
  232. // With regards to proceess values:
  233. // 0xFFFF8001 is a handle alias for the current KProcess, and 0xFFFF8000 is a handle alias for
  234. // the current KThread.
  235. *resource_limit = 0xDEADBEEF;
  236. LOG_ERROR(Kernel_SVC, "(UNIMPLEMENTED) called process=0x%08X", process);
  237. return RESULT_SUCCESS;
  238. }
  239. /// Get resource limit current values
  240. static ResultCode GetResourceLimitCurrentValues(s64* values, Handle resource_limit, void* names,
  241. s32 name_count) {
  242. LOG_ERROR(Kernel_SVC, "(UNIMPLEMENTED) called resource_limit=%08X, names=%s, name_count=%d",
  243. resource_limit, names, name_count);
  244. Memory::Write32(Core::g_app_core->GetReg(0), 0); // Normmatt: Set used memory to 0 for now
  245. return RESULT_SUCCESS;
  246. }
  247. /// Creates a new thread
  248. static ResultCode CreateThread(u32* out_handle, u32 priority, u32 entry_point, u32 arg, u32 stack_top, u32 processor_id) {
  249. using Kernel::Thread;
  250. std::string name;
  251. if (Symbols::HasSymbol(entry_point)) {
  252. TSymbol symbol = Symbols::GetSymbol(entry_point);
  253. name = symbol.name;
  254. } else {
  255. name = Common::StringFromFormat("unknown-%08x", entry_point);
  256. }
  257. CASCADE_RESULT(SharedPtr<Thread> thread, Kernel::Thread::Create(
  258. name, entry_point, priority, arg, processor_id, stack_top));
  259. CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(std::move(thread)));
  260. LOG_TRACE(Kernel_SVC, "called entrypoint=0x%08X (%s), arg=0x%08X, stacktop=0x%08X, "
  261. "threadpriority=0x%08X, processorid=0x%08X : created handle=0x%08X", entry_point,
  262. name.c_str(), arg, stack_top, priority, processor_id, *out_handle);
  263. if (THREADPROCESSORID_1 == processor_id) {
  264. LOG_WARNING(Kernel_SVC,
  265. "thread designated for system CPU core (UNIMPLEMENTED) will be run with app core scheduling");
  266. }
  267. return RESULT_SUCCESS;
  268. }
  269. /// Called when a thread exits
  270. static void ExitThread() {
  271. LOG_TRACE(Kernel_SVC, "called, pc=0x%08X", Core::g_app_core->GetPC());
  272. Kernel::GetCurrentThread()->Stop();
  273. HLE::Reschedule(__func__);
  274. }
  275. /// Gets the priority for the specified thread
  276. static ResultCode GetThreadPriority(s32* priority, Handle handle) {
  277. const SharedPtr<Kernel::Thread> thread = Kernel::g_handle_table.Get<Kernel::Thread>(handle);
  278. if (thread == nullptr)
  279. return ERR_INVALID_HANDLE;
  280. *priority = thread->GetPriority();
  281. return RESULT_SUCCESS;
  282. }
  283. /// Sets the priority for the specified thread
  284. static ResultCode SetThreadPriority(Handle handle, s32 priority) {
  285. SharedPtr<Kernel::Thread> thread = Kernel::g_handle_table.Get<Kernel::Thread>(handle);
  286. if (thread == nullptr)
  287. return ERR_INVALID_HANDLE;
  288. thread->SetPriority(priority);
  289. return RESULT_SUCCESS;
  290. }
  291. /// Create a mutex
  292. static ResultCode CreateMutex(Handle* out_handle, u32 initial_locked) {
  293. using Kernel::Mutex;
  294. SharedPtr<Mutex> mutex = Mutex::Create(initial_locked != 0);
  295. CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(std::move(mutex)));
  296. LOG_TRACE(Kernel_SVC, "called initial_locked=%s : created handle=0x%08X",
  297. initial_locked ? "true" : "false", *out_handle);
  298. return RESULT_SUCCESS;
  299. }
  300. /// Release a mutex
  301. static ResultCode ReleaseMutex(Handle handle) {
  302. using Kernel::Mutex;
  303. LOG_TRACE(Kernel_SVC, "called handle=0x%08X", handle);
  304. SharedPtr<Mutex> mutex = Kernel::g_handle_table.Get<Mutex>(handle);
  305. if (mutex == nullptr)
  306. return ERR_INVALID_HANDLE;
  307. mutex->Release();
  308. return RESULT_SUCCESS;
  309. }
  310. /// Get the ID for the specified thread.
  311. static ResultCode GetThreadId(u32* thread_id, Handle handle) {
  312. LOG_TRACE(Kernel_SVC, "called thread=0x%08X", handle);
  313. const SharedPtr<Kernel::Thread> thread = Kernel::g_handle_table.Get<Kernel::Thread>(handle);
  314. if (thread == nullptr)
  315. return ERR_INVALID_HANDLE;
  316. *thread_id = thread->GetThreadId();
  317. return RESULT_SUCCESS;
  318. }
  319. /// Creates a semaphore
  320. static ResultCode CreateSemaphore(Handle* out_handle, s32 initial_count, s32 max_count) {
  321. using Kernel::Semaphore;
  322. CASCADE_RESULT(SharedPtr<Semaphore> semaphore, Semaphore::Create(initial_count, max_count));
  323. CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(std::move(semaphore)));
  324. LOG_TRACE(Kernel_SVC, "called initial_count=%d, max_count=%d, created handle=0x%08X",
  325. initial_count, max_count, *out_handle);
  326. return RESULT_SUCCESS;
  327. }
  328. /// Releases a certain number of slots in a semaphore
  329. static ResultCode ReleaseSemaphore(s32* count, Handle handle, s32 release_count) {
  330. using Kernel::Semaphore;
  331. LOG_TRACE(Kernel_SVC, "called release_count=%d, handle=0x%08X", release_count, handle);
  332. SharedPtr<Semaphore> semaphore = Kernel::g_handle_table.Get<Semaphore>(handle);
  333. if (semaphore == nullptr)
  334. return ERR_INVALID_HANDLE;
  335. CASCADE_RESULT(*count, semaphore->Release(release_count));
  336. return RESULT_SUCCESS;
  337. }
  338. /// Query memory
  339. static ResultCode QueryMemory(void* info, void* out, u32 addr) {
  340. LOG_ERROR(Kernel_SVC, "(UNIMPLEMENTED) called addr=0x%08X", addr);
  341. return RESULT_SUCCESS;
  342. }
  343. /// Create an event
  344. static ResultCode CreateEvent(Handle* out_handle, u32 reset_type) {
  345. using Kernel::Event;
  346. SharedPtr<Event> evt = Kernel::Event::Create(static_cast<ResetType>(reset_type));
  347. CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(std::move(evt)));
  348. LOG_TRACE(Kernel_SVC, "called reset_type=0x%08X : created handle=0x%08X",
  349. reset_type, *out_handle);
  350. return RESULT_SUCCESS;
  351. }
  352. /// Duplicates a kernel handle
  353. static ResultCode DuplicateHandle(Handle* out, Handle handle) {
  354. CASCADE_RESULT(*out, Kernel::g_handle_table.Duplicate(handle));
  355. LOG_TRACE(Kernel_SVC, "duplicated 0x%08X to 0x%08X", handle, *out);
  356. return RESULT_SUCCESS;
  357. }
  358. /// Signals an event
  359. static ResultCode SignalEvent(Handle handle) {
  360. using Kernel::Event;
  361. LOG_TRACE(Kernel_SVC, "called event=0x%08X", handle);
  362. SharedPtr<Event> evt = Kernel::g_handle_table.Get<Kernel::Event>(handle);
  363. if (evt == nullptr)
  364. return ERR_INVALID_HANDLE;
  365. evt->Signal();
  366. HLE::Reschedule(__func__);
  367. return RESULT_SUCCESS;
  368. }
  369. /// Clears an event
  370. static ResultCode ClearEvent(Handle handle) {
  371. using Kernel::Event;
  372. LOG_TRACE(Kernel_SVC, "called event=0x%08X", handle);
  373. SharedPtr<Event> evt = Kernel::g_handle_table.Get<Kernel::Event>(handle);
  374. if (evt == nullptr)
  375. return ERR_INVALID_HANDLE;
  376. evt->Clear();
  377. return RESULT_SUCCESS;
  378. }
  379. /// Creates a timer
  380. static ResultCode CreateTimer(Handle* out_handle, u32 reset_type) {
  381. using Kernel::Timer;
  382. SharedPtr<Timer> timer = Timer::Create(static_cast<ResetType>(reset_type));
  383. CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(std::move(timer)));
  384. LOG_TRACE(Kernel_SVC, "called reset_type=0x%08X : created handle=0x%08X",
  385. reset_type, *out_handle);
  386. return RESULT_SUCCESS;
  387. }
  388. /// Clears a timer
  389. static ResultCode ClearTimer(Handle handle) {
  390. using Kernel::Timer;
  391. LOG_TRACE(Kernel_SVC, "called timer=0x%08X", handle);
  392. SharedPtr<Timer> timer = Kernel::g_handle_table.Get<Timer>(handle);
  393. if (timer == nullptr)
  394. return ERR_INVALID_HANDLE;
  395. timer->Clear();
  396. return RESULT_SUCCESS;
  397. }
  398. /// Starts a timer
  399. static ResultCode SetTimer(Handle handle, s64 initial, s64 interval) {
  400. using Kernel::Timer;
  401. LOG_TRACE(Kernel_SVC, "called timer=0x%08X", handle);
  402. SharedPtr<Timer> timer = Kernel::g_handle_table.Get<Timer>(handle);
  403. if (timer == nullptr)
  404. return ERR_INVALID_HANDLE;
  405. timer->Set(initial, interval);
  406. return RESULT_SUCCESS;
  407. }
  408. /// Cancels a timer
  409. static ResultCode CancelTimer(Handle handle) {
  410. using Kernel::Timer;
  411. LOG_TRACE(Kernel_SVC, "called timer=0x%08X", handle);
  412. SharedPtr<Timer> timer = Kernel::g_handle_table.Get<Timer>(handle);
  413. if (timer == nullptr)
  414. return ERR_INVALID_HANDLE;
  415. timer->Cancel();
  416. return RESULT_SUCCESS;
  417. }
  418. /// Sleep the current thread
  419. static void SleepThread(s64 nanoseconds) {
  420. LOG_TRACE(Kernel_SVC, "called nanoseconds=%lld", nanoseconds);
  421. // Sleep current thread and check for next thread to schedule
  422. Kernel::WaitCurrentThread_Sleep();
  423. // Create an event to wake the thread up after the specified nanosecond delay has passed
  424. Kernel::GetCurrentThread()->WakeAfterDelay(nanoseconds);
  425. HLE::Reschedule(__func__);
  426. }
  427. /// This returns the total CPU ticks elapsed since the CPU was powered-on
  428. static s64 GetSystemTick() {
  429. return (s64)Core::g_app_core->GetTicks();
  430. }
  431. /// Creates a memory block at the specified address with the specified permissions and size
  432. static ResultCode CreateMemoryBlock(Handle* out_handle, u32 addr, u32 size, u32 my_permission,
  433. u32 other_permission) {
  434. using Kernel::SharedMemory;
  435. // TODO(Subv): Implement this function
  436. SharedPtr<SharedMemory> shared_memory = SharedMemory::Create();
  437. CASCADE_RESULT(*out_handle, Kernel::g_handle_table.Create(std::move(shared_memory)));
  438. LOG_WARNING(Kernel_SVC, "(STUBBED) called addr=0x%08X", addr);
  439. return RESULT_SUCCESS;
  440. }
  441. const HLE::FunctionDef SVC_Table[] = {
  442. {0x00, nullptr, "Unknown"},
  443. {0x01, HLE::Wrap<ControlMemory>, "ControlMemory"},
  444. {0x02, HLE::Wrap<QueryMemory>, "QueryMemory"},
  445. {0x03, nullptr, "ExitProcess"},
  446. {0x04, nullptr, "GetProcessAffinityMask"},
  447. {0x05, nullptr, "SetProcessAffinityMask"},
  448. {0x06, nullptr, "GetProcessIdealProcessor"},
  449. {0x07, nullptr, "SetProcessIdealProcessor"},
  450. {0x08, HLE::Wrap<CreateThread>, "CreateThread"},
  451. {0x09, ExitThread, "ExitThread"},
  452. {0x0A, HLE::Wrap<SleepThread>, "SleepThread"},
  453. {0x0B, HLE::Wrap<GetThreadPriority>, "GetThreadPriority"},
  454. {0x0C, HLE::Wrap<SetThreadPriority>, "SetThreadPriority"},
  455. {0x0D, nullptr, "GetThreadAffinityMask"},
  456. {0x0E, nullptr, "SetThreadAffinityMask"},
  457. {0x0F, nullptr, "GetThreadIdealProcessor"},
  458. {0x10, nullptr, "SetThreadIdealProcessor"},
  459. {0x11, nullptr, "GetCurrentProcessorNumber"},
  460. {0x12, nullptr, "Run"},
  461. {0x13, HLE::Wrap<CreateMutex>, "CreateMutex"},
  462. {0x14, HLE::Wrap<ReleaseMutex>, "ReleaseMutex"},
  463. {0x15, HLE::Wrap<CreateSemaphore>, "CreateSemaphore"},
  464. {0x16, HLE::Wrap<ReleaseSemaphore>, "ReleaseSemaphore"},
  465. {0x17, HLE::Wrap<CreateEvent>, "CreateEvent"},
  466. {0x18, HLE::Wrap<SignalEvent>, "SignalEvent"},
  467. {0x19, HLE::Wrap<ClearEvent>, "ClearEvent"},
  468. {0x1A, HLE::Wrap<CreateTimer>, "CreateTimer"},
  469. {0x1B, HLE::Wrap<SetTimer>, "SetTimer"},
  470. {0x1C, HLE::Wrap<CancelTimer>, "CancelTimer"},
  471. {0x1D, HLE::Wrap<ClearTimer>, "ClearTimer"},
  472. {0x1E, HLE::Wrap<CreateMemoryBlock>, "CreateMemoryBlock"},
  473. {0x1F, HLE::Wrap<MapMemoryBlock>, "MapMemoryBlock"},
  474. {0x20, nullptr, "UnmapMemoryBlock"},
  475. {0x21, HLE::Wrap<CreateAddressArbiter>, "CreateAddressArbiter"},
  476. {0x22, HLE::Wrap<ArbitrateAddress>, "ArbitrateAddress"},
  477. {0x23, HLE::Wrap<CloseHandle>, "CloseHandle"},
  478. {0x24, HLE::Wrap<WaitSynchronization1>, "WaitSynchronization1"},
  479. {0x25, HLE::Wrap<WaitSynchronizationN>, "WaitSynchronizationN"},
  480. {0x26, nullptr, "SignalAndWait"},
  481. {0x27, HLE::Wrap<DuplicateHandle>, "DuplicateHandle"},
  482. {0x28, HLE::Wrap<GetSystemTick>, "GetSystemTick"},
  483. {0x29, nullptr, "GetHandleInfo"},
  484. {0x2A, nullptr, "GetSystemInfo"},
  485. {0x2B, nullptr, "GetProcessInfo"},
  486. {0x2C, nullptr, "GetThreadInfo"},
  487. {0x2D, HLE::Wrap<ConnectToPort>, "ConnectToPort"},
  488. {0x2E, nullptr, "SendSyncRequest1"},
  489. {0x2F, nullptr, "SendSyncRequest2"},
  490. {0x30, nullptr, "SendSyncRequest3"},
  491. {0x31, nullptr, "SendSyncRequest4"},
  492. {0x32, HLE::Wrap<SendSyncRequest>, "SendSyncRequest"},
  493. {0x33, nullptr, "OpenProcess"},
  494. {0x34, nullptr, "OpenThread"},
  495. {0x35, nullptr, "GetProcessId"},
  496. {0x36, nullptr, "GetProcessIdOfThread"},
  497. {0x37, HLE::Wrap<GetThreadId>, "GetThreadId"},
  498. {0x38, HLE::Wrap<GetResourceLimit>, "GetResourceLimit"},
  499. {0x39, nullptr, "GetResourceLimitLimitValues"},
  500. {0x3A, HLE::Wrap<GetResourceLimitCurrentValues>, "GetResourceLimitCurrentValues"},
  501. {0x3B, nullptr, "GetThreadContext"},
  502. {0x3C, nullptr, "Break"},
  503. {0x3D, HLE::Wrap<OutputDebugString>, "OutputDebugString"},
  504. {0x3E, nullptr, "ControlPerformanceCounter"},
  505. {0x3F, nullptr, "Unknown"},
  506. {0x40, nullptr, "Unknown"},
  507. {0x41, nullptr, "Unknown"},
  508. {0x42, nullptr, "Unknown"},
  509. {0x43, nullptr, "Unknown"},
  510. {0x44, nullptr, "Unknown"},
  511. {0x45, nullptr, "Unknown"},
  512. {0x46, nullptr, "Unknown"},
  513. {0x47, nullptr, "CreatePort"},
  514. {0x48, nullptr, "CreateSessionToPort"},
  515. {0x49, nullptr, "CreateSession"},
  516. {0x4A, nullptr, "AcceptSession"},
  517. {0x4B, nullptr, "ReplyAndReceive1"},
  518. {0x4C, nullptr, "ReplyAndReceive2"},
  519. {0x4D, nullptr, "ReplyAndReceive3"},
  520. {0x4E, nullptr, "ReplyAndReceive4"},
  521. {0x4F, nullptr, "ReplyAndReceive"},
  522. {0x50, nullptr, "BindInterrupt"},
  523. {0x51, nullptr, "UnbindInterrupt"},
  524. {0x52, nullptr, "InvalidateProcessDataCache"},
  525. {0x53, nullptr, "StoreProcessDataCache"},
  526. {0x54, nullptr, "FlushProcessDataCache"},
  527. {0x55, nullptr, "StartInterProcessDma"},
  528. {0x56, nullptr, "StopDma"},
  529. {0x57, nullptr, "GetDmaState"},
  530. {0x58, nullptr, "RestartDma"},
  531. {0x59, nullptr, "Unknown"},
  532. {0x5A, nullptr, "Unknown"},
  533. {0x5B, nullptr, "Unknown"},
  534. {0x5C, nullptr, "Unknown"},
  535. {0x5D, nullptr, "Unknown"},
  536. {0x5E, nullptr, "Unknown"},
  537. {0x5F, nullptr, "Unknown"},
  538. {0x60, nullptr, "DebugActiveProcess"},
  539. {0x61, nullptr, "BreakDebugProcess"},
  540. {0x62, nullptr, "TerminateDebugProcess"},
  541. {0x63, nullptr, "GetProcessDebugEvent"},
  542. {0x64, nullptr, "ContinueDebugEvent"},
  543. {0x65, nullptr, "GetProcessList"},
  544. {0x66, nullptr, "GetThreadList"},
  545. {0x67, nullptr, "GetDebugThreadContext"},
  546. {0x68, nullptr, "SetDebugThreadContext"},
  547. {0x69, nullptr, "QueryDebugProcessMemory"},
  548. {0x6A, nullptr, "ReadProcessMemory"},
  549. {0x6B, nullptr, "WriteProcessMemory"},
  550. {0x6C, nullptr, "SetHardwareBreakPoint"},
  551. {0x6D, nullptr, "GetDebugThreadParam"},
  552. {0x6E, nullptr, "Unknown"},
  553. {0x6F, nullptr, "Unknown"},
  554. {0x70, nullptr, "ControlProcessMemory"},
  555. {0x71, nullptr, "MapProcessMemory"},
  556. {0x72, nullptr, "UnmapProcessMemory"},
  557. {0x73, nullptr, "CreateCodeSet"},
  558. {0x74, nullptr, "RandomStub"},
  559. {0x75, nullptr, "CreateProcess"},
  560. {0x76, nullptr, "TerminateProcess"},
  561. {0x77, nullptr, "SetProcessResourceLimits"},
  562. {0x78, nullptr, "CreateResourceLimit"},
  563. {0x79, nullptr, "SetResourceLimitValues"},
  564. {0x7A, nullptr, "AddCodeSegment"},
  565. {0x7B, nullptr, "Backdoor"},
  566. {0x7C, nullptr, "KernelSetState"},
  567. {0x7D, nullptr, "QueryProcessMemory"},
  568. };
  569. void Register() {
  570. HLE::RegisterModule("SVC_Table", ARRAY_SIZE(SVC_Table), SVC_Table);
  571. }
  572. } // namespace