svc.cpp 28 KB

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