svc.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/logging/log.h"
  5. #include "common/microprofile.h"
  6. #include "common/string_util.h"
  7. #include "core/core_timing.h"
  8. #include "core/hle/kernel/client_port.h"
  9. #include "core/hle/kernel/client_session.h"
  10. #include "core/hle/kernel/handle_table.h"
  11. #include "core/hle/kernel/mutex.h"
  12. #include "core/hle/kernel/object_address_table.h"
  13. #include "core/hle/kernel/process.h"
  14. #include "core/hle/kernel/resource_limit.h"
  15. #include "core/hle/kernel/svc.h"
  16. #include "core/hle/kernel/svc_wrap.h"
  17. #include "core/hle/kernel/sync_object.h"
  18. #include "core/hle/kernel/thread.h"
  19. #include "core/hle/lock.h"
  20. #include "core/hle/result.h"
  21. #include "core/hle/service/service.h"
  22. namespace Kernel {
  23. /// Set the process heap to a given Size. It can both extend and shrink the heap.
  24. static ResultCode SetHeapSize(VAddr* heap_addr, u64 heap_size) {
  25. LOG_TRACE(Kernel_SVC, "called, heap_size=0x%llx", heap_size);
  26. auto& process = *g_current_process;
  27. CASCADE_RESULT(*heap_addr,
  28. process.HeapAllocate(Memory::HEAP_VADDR, heap_size, VMAPermission::ReadWrite));
  29. return RESULT_SUCCESS;
  30. }
  31. /// Maps a memory range into a different range.
  32. static ResultCode MapMemory(VAddr dst_addr, VAddr src_addr, u64 size) {
  33. LOG_TRACE(Kernel_SVC, "called, dst_addr=0x%llx, src_addr=0x%llx, size=0x%llx", dst_addr,
  34. src_addr, size);
  35. return g_current_process->MirrorMemory(dst_addr, src_addr, size);
  36. }
  37. /// Unmaps a region that was previously mapped with svcMapMemory
  38. static ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size) {
  39. LOG_TRACE(Kernel_SVC, "called, dst_addr=0x%llx, src_addr=0x%llx, size=0x%llx", dst_addr,
  40. src_addr, size);
  41. return g_current_process->UnmapMemory(dst_addr, src_addr, size);
  42. }
  43. /// Connect to an OS service given the port name, returns the handle to the port to out
  44. static ResultCode ConnectToPort(Handle* out_handle, VAddr port_name_address) {
  45. if (!Memory::IsValidVirtualAddress(port_name_address))
  46. return ERR_NOT_FOUND;
  47. static constexpr std::size_t PortNameMaxLength = 11;
  48. // Read 1 char beyond the max allowed port name to detect names that are too long.
  49. std::string port_name = Memory::ReadCString(port_name_address, PortNameMaxLength + 1);
  50. if (port_name.size() > PortNameMaxLength)
  51. return ERR_PORT_NAME_TOO_LONG;
  52. LOG_TRACE(Kernel_SVC, "called port_name=%s", port_name.c_str());
  53. auto it = Service::g_kernel_named_ports.find(port_name);
  54. if (it == Service::g_kernel_named_ports.end()) {
  55. LOG_WARNING(Kernel_SVC, "tried to connect to unknown port: %s", port_name.c_str());
  56. return ERR_NOT_FOUND;
  57. }
  58. auto client_port = it->second;
  59. SharedPtr<ClientSession> client_session;
  60. CASCADE_RESULT(client_session, client_port->Connect());
  61. // Return the client session
  62. CASCADE_RESULT(*out_handle, g_handle_table.Create(client_session));
  63. return RESULT_SUCCESS;
  64. }
  65. /// Makes a blocking IPC call to an OS service.
  66. static ResultCode SendSyncRequest(Handle handle) {
  67. SharedPtr<SyncObject> session = g_handle_table.Get<SyncObject>(handle);
  68. if (!session) {
  69. LOG_ERROR(Kernel_SVC, "called with invalid handle=0x%08X", handle);
  70. return ERR_INVALID_HANDLE;
  71. }
  72. LOG_TRACE(Kernel_SVC, "called handle=0x%08X(%s)", handle, session->GetName().c_str());
  73. Core::System::GetInstance().PrepareReschedule();
  74. // TODO(Subv): svcSendSyncRequest should put the caller thread to sleep while the server
  75. // responds and cause a reschedule.
  76. return session->SendSyncRequest(GetCurrentThread());
  77. }
  78. /// Get the ID for the specified thread.
  79. static ResultCode GetThreadId(u32* thread_id, Handle thread_handle) {
  80. LOG_TRACE(Kernel_SVC, "called thread=0x%08X", thread_handle);
  81. const SharedPtr<Thread> thread = g_handle_table.Get<Thread>(thread_handle);
  82. if (!thread) {
  83. return ERR_INVALID_HANDLE;
  84. }
  85. *thread_id = thread->GetThreadId();
  86. return RESULT_SUCCESS;
  87. }
  88. /// Get the ID of the specified process
  89. static ResultCode GetProcessId(u32* process_id, Handle process_handle) {
  90. LOG_TRACE(Kernel_SVC, "called process=0x%08X", process_handle);
  91. const SharedPtr<Process> process = g_handle_table.Get<Process>(process_handle);
  92. if (!process) {
  93. return ERR_INVALID_HANDLE;
  94. }
  95. *process_id = process->process_id;
  96. return RESULT_SUCCESS;
  97. }
  98. /// Wait for the given handles to synchronize, timeout after the specified nanoseconds
  99. static ResultCode WaitSynchronization(VAddr handles_address, u64 handle_count, s64 nano_seconds) {
  100. LOG_WARNING(Kernel_SVC,
  101. "(STUBBED) called handles_address=0x%llx, handle_count=%d, nano_seconds=%d",
  102. handles_address, handle_count, nano_seconds);
  103. return RESULT_SUCCESS;
  104. }
  105. /// Attempts to locks a mutex, creating it if it does not already exist
  106. static ResultCode LockMutex(Handle holding_thread_handle, VAddr mutex_addr,
  107. Handle requesting_thread_handle) {
  108. LOG_TRACE(Kernel_SVC,
  109. "called holding_thread_handle=0x%08X, mutex_addr=0x%llx, "
  110. "requesting_current_thread_handle=0x%08X",
  111. holding_thread_handle, mutex_addr, requesting_thread_handle);
  112. SharedPtr<Thread> holding_thread = g_handle_table.Get<Thread>(holding_thread_handle);
  113. SharedPtr<Thread> requesting_thread = g_handle_table.Get<Thread>(requesting_thread_handle);
  114. ASSERT(holding_thread);
  115. ASSERT(requesting_thread);
  116. SharedPtr<Mutex> mutex = g_object_address_table.Get<Mutex>(mutex_addr);
  117. if (!mutex) {
  118. // Create a new mutex for the specified address if one does not already exist
  119. mutex = Mutex::Create(holding_thread, mutex_addr);
  120. mutex->name = Common::StringFromFormat("mutex-%llx", mutex_addr);
  121. }
  122. if (mutex->ShouldWait(requesting_thread.get())) {
  123. // If we cannot lock the mutex, then put the thread too sleep and trigger a reschedule
  124. requesting_thread->wait_objects = {mutex};
  125. mutex->AddWaitingThread(requesting_thread);
  126. requesting_thread->status = THREADSTATUS_WAIT_SYNCH_ANY;
  127. Core::System::GetInstance().PrepareReschedule();
  128. } else {
  129. // The mutex is available, lock it
  130. mutex->Acquire(requesting_thread.get());
  131. }
  132. return RESULT_SUCCESS;
  133. }
  134. /// Unlock a mutex
  135. static ResultCode UnlockMutex(VAddr mutex_addr) {
  136. LOG_TRACE(Kernel_SVC, "called mutex_addr=0x%llx", mutex_addr);
  137. SharedPtr<Mutex> mutex = g_object_address_table.Get<Mutex>(mutex_addr);
  138. ASSERT(mutex);
  139. return mutex->Release(GetCurrentThread());
  140. }
  141. /// Break program execution
  142. static void Break(u64 unk_0, u64 unk_1, u64 unk_2) {
  143. LOG_CRITICAL(Debug_Emulated, "Emulated program broke execution!");
  144. ASSERT(false);
  145. }
  146. /// Used to output a message on a debug hardware unit - does nothing on a retail unit
  147. static void OutputDebugString(VAddr address, s32 len) {
  148. std::vector<char> string(len);
  149. Memory::ReadBlock(address, string.data(), len);
  150. LOG_DEBUG(Debug_Emulated, "%.*s", len, string.data());
  151. }
  152. /// Gets system/memory information for the current process
  153. static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) {
  154. LOG_TRACE(Kernel_SVC, "called info_id=0x%X, info_sub_id=0x%X, handle=0x%08X", info_id,
  155. info_sub_id, handle);
  156. auto& vm_manager = g_current_process->vm_manager;
  157. switch (static_cast<GetInfoType>(info_id)) {
  158. case GetInfoType::TotalMemoryUsage:
  159. *result = vm_manager.GetTotalMemoryUsage();
  160. break;
  161. case GetInfoType::TotalHeapUsage:
  162. *result = vm_manager.GetTotalHeapUsage();
  163. break;
  164. case GetInfoType::RandomEntropy:
  165. *result = 0;
  166. break;
  167. case GetInfoType::AddressSpaceBaseAddr:
  168. *result = vm_manager.GetAddressSpaceBaseAddr();
  169. break;
  170. case GetInfoType::AddressSpaceSize:
  171. *result = vm_manager.GetAddressSpaceSize();
  172. break;
  173. case GetInfoType::NewMapRegionBaseAddr:
  174. *result = vm_manager.GetNewMapRegionBaseAddr();
  175. break;
  176. case GetInfoType::NewMapRegionSize:
  177. *result = vm_manager.GetNewMapRegionSize();
  178. break;
  179. default:
  180. UNIMPLEMENTED();
  181. }
  182. return RESULT_SUCCESS;
  183. }
  184. /// Gets the priority for the specified thread
  185. static ResultCode GetThreadPriority(u32* priority, Handle handle) {
  186. const SharedPtr<Thread> thread = g_handle_table.Get<Thread>(handle);
  187. if (!thread)
  188. return ERR_INVALID_HANDLE;
  189. *priority = thread->GetPriority();
  190. return RESULT_SUCCESS;
  191. }
  192. /// Sets the priority for the specified thread
  193. static ResultCode SetThreadPriority(Handle handle, u32 priority) {
  194. if (priority > THREADPRIO_LOWEST) {
  195. return ERR_OUT_OF_RANGE;
  196. }
  197. SharedPtr<Thread> thread = g_handle_table.Get<Thread>(handle);
  198. if (!thread)
  199. return ERR_INVALID_HANDLE;
  200. // Note: The kernel uses the current process's resource limit instead of
  201. // the one from the thread owner's resource limit.
  202. SharedPtr<ResourceLimit>& resource_limit = g_current_process->resource_limit;
  203. if (resource_limit->GetMaxResourceValue(ResourceTypes::PRIORITY) > priority) {
  204. return ERR_NOT_AUTHORIZED;
  205. }
  206. thread->SetPriority(priority);
  207. thread->UpdatePriority();
  208. // Update the mutexes that this thread is waiting for
  209. for (auto& mutex : thread->pending_mutexes)
  210. mutex->UpdatePriority();
  211. Core::System::GetInstance().PrepareReschedule();
  212. return RESULT_SUCCESS;
  213. }
  214. /// Get which CPU core is executing the current thread
  215. static u32 GetCurrentProcessorNumber() {
  216. LOG_WARNING(Kernel_SVC, "(STUBBED) called, defaulting to processor 0");
  217. return 0;
  218. }
  219. /// Query process memory
  220. static ResultCode QueryProcessMemory(MemoryInfo* memory_info, PageInfo* /*page_info*/,
  221. Handle process_handle, u64 addr) {
  222. SharedPtr<Process> process = g_handle_table.Get<Process>(process_handle);
  223. if (!process) {
  224. return ERR_INVALID_HANDLE;
  225. }
  226. auto vma = process->vm_manager.FindVMA(addr);
  227. memory_info->attributes = 0;
  228. if (vma == g_current_process->vm_manager.vma_map.end()) {
  229. memory_info->base_address = 0;
  230. memory_info->permission = static_cast<u32>(VMAPermission::None);
  231. memory_info->size = 0;
  232. memory_info->type = static_cast<u32>(MemoryState::Free);
  233. } else {
  234. memory_info->base_address = vma->second.base;
  235. memory_info->permission = static_cast<u32>(vma->second.permissions);
  236. memory_info->size = vma->second.size;
  237. memory_info->type = static_cast<u32>(vma->second.meminfo_state);
  238. }
  239. LOG_TRACE(Kernel_SVC, "called process=0x%08X addr=%llx", process_handle, addr);
  240. return RESULT_SUCCESS;
  241. }
  242. /// Query memory
  243. static ResultCode QueryMemory(MemoryInfo* memory_info, PageInfo* page_info, VAddr addr) {
  244. LOG_TRACE(Kernel_SVC, "called, addr=%llx", addr);
  245. return QueryProcessMemory(memory_info, page_info, CurrentProcess, addr);
  246. }
  247. /// Exits the current process
  248. static void ExitProcess() {
  249. LOG_INFO(Kernel_SVC, "Process %u exiting", g_current_process->process_id);
  250. ASSERT_MSG(g_current_process->status == ProcessStatus::Running, "Process has already exited");
  251. g_current_process->status = ProcessStatus::Exited;
  252. // Stop all the process threads that are currently waiting for objects.
  253. auto& thread_list = GetThreadList();
  254. for (auto& thread : thread_list) {
  255. if (thread->owner_process != g_current_process)
  256. continue;
  257. if (thread == GetCurrentThread())
  258. continue;
  259. // TODO(Subv): When are the other running/ready threads terminated?
  260. ASSERT_MSG(thread->status == THREADSTATUS_WAIT_SYNCH_ANY ||
  261. thread->status == THREADSTATUS_WAIT_SYNCH_ALL,
  262. "Exiting processes with non-waiting threads is currently unimplemented");
  263. thread->Stop();
  264. }
  265. // Kill the current thread
  266. GetCurrentThread()->Stop();
  267. Core::System::GetInstance().PrepareReschedule();
  268. }
  269. /// Creates a new thread
  270. static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, VAddr stack_top,
  271. u32 priority, s32 processor_id) {
  272. std::string name = Common::StringFromFormat("unknown-%llx", entry_point);
  273. if (priority > THREADPRIO_LOWEST) {
  274. return ERR_OUT_OF_RANGE;
  275. }
  276. SharedPtr<ResourceLimit>& resource_limit = g_current_process->resource_limit;
  277. if (resource_limit->GetMaxResourceValue(ResourceTypes::PRIORITY) > priority) {
  278. return ERR_NOT_AUTHORIZED;
  279. }
  280. if (processor_id == THREADPROCESSORID_DEFAULT) {
  281. // Set the target CPU to the one specified in the process' exheader.
  282. processor_id = g_current_process->ideal_processor;
  283. ASSERT(processor_id != THREADPROCESSORID_DEFAULT);
  284. }
  285. switch (processor_id) {
  286. case THREADPROCESSORID_0:
  287. break;
  288. case THREADPROCESSORID_ALL:
  289. LOG_INFO(Kernel_SVC,
  290. "Newly created thread is allowed to be run in any Core, unimplemented.");
  291. break;
  292. case THREADPROCESSORID_1:
  293. LOG_ERROR(Kernel_SVC,
  294. "Newly created thread must run in the SysCore (Core1), unimplemented.");
  295. break;
  296. default:
  297. // TODO(bunnei): Implement support for other processor IDs
  298. ASSERT_MSG(false, "Unsupported thread processor ID: %d", processor_id);
  299. break;
  300. }
  301. CASCADE_RESULT(SharedPtr<Thread> thread,
  302. Thread::Create(name, entry_point, priority, arg, processor_id, stack_top,
  303. g_current_process));
  304. CASCADE_RESULT(thread->guest_handle, g_handle_table.Create(thread));
  305. *out_handle = thread->guest_handle;
  306. Core::System::GetInstance().PrepareReschedule();
  307. LOG_TRACE(Kernel_SVC,
  308. "called entrypoint=0x%08X (%s), arg=0x%08X, stacktop=0x%08X, "
  309. "threadpriority=0x%08X, processorid=0x%08X : created handle=0x%08X",
  310. entry_point, name.c_str(), arg, stack_top, priority, processor_id, *out_handle);
  311. return RESULT_SUCCESS;
  312. }
  313. /// Starts the thread for the provided handle
  314. static ResultCode StartThread(Handle thread_handle) {
  315. LOG_TRACE(Kernel_SVC, "called thread=0x%08X", thread_handle);
  316. const SharedPtr<Thread> thread = g_handle_table.Get<Thread>(thread_handle);
  317. if (!thread) {
  318. return ERR_INVALID_HANDLE;
  319. }
  320. thread->ResumeFromWait();
  321. return RESULT_SUCCESS;
  322. }
  323. /// Called when a thread exits
  324. static void ExitThread() {
  325. LOG_TRACE(Kernel_SVC, "called, pc=0x%08X", Core::CPU().GetPC());
  326. ExitCurrentThread();
  327. Core::System::GetInstance().PrepareReschedule();
  328. }
  329. /// Sleep the current thread
  330. static void SleepThread(s64 nanoseconds) {
  331. LOG_TRACE(Kernel_SVC, "called nanoseconds=%lld", nanoseconds);
  332. // Don't attempt to yield execution if there are no available threads to run,
  333. // this way we avoid a useless reschedule to the idle thread.
  334. if (nanoseconds == 0 && !HaveReadyThreads())
  335. return;
  336. // Sleep current thread and check for next thread to schedule
  337. WaitCurrentThread_Sleep();
  338. // Create an event to wake the thread up after the specified nanosecond delay has passed
  339. GetCurrentThread()->WakeAfterDelay(nanoseconds);
  340. Core::System::GetInstance().PrepareReschedule();
  341. }
  342. /// Signal process wide key
  343. static ResultCode SignalProcessWideKey(VAddr addr, u32 target) {
  344. LOG_WARNING(Kernel_SVC, "(STUBBED) called, address=0x%llx, target=0x%08x", addr, target);
  345. return RESULT_SUCCESS;
  346. }
  347. /// Close a handle
  348. static ResultCode CloseHandle(Handle handle) {
  349. LOG_TRACE(Kernel_SVC, "Closing handle 0x%08X", handle);
  350. return g_handle_table.Close(handle);
  351. }
  352. namespace {
  353. struct FunctionDef {
  354. using Func = void();
  355. u32 id;
  356. Func* func;
  357. const char* name;
  358. };
  359. } // namespace
  360. static const FunctionDef SVC_Table[] = {
  361. {0x00, nullptr, "Unknown"},
  362. {0x01, SvcWrap<SetHeapSize>, "SetHeapSize"},
  363. {0x02, nullptr, "SetMemoryPermission"},
  364. {0x03, nullptr, "SetMemoryAttribute"},
  365. {0x04, SvcWrap<MapMemory>, "MapMemory"},
  366. {0x05, SvcWrap<UnmapMemory>, "UnmapMemory"},
  367. {0x06, SvcWrap<QueryMemory>, "QueryMemory"},
  368. {0x07, SvcWrap<ExitProcess>, "ExitProcess"},
  369. {0x08, SvcWrap<CreateThread>, "CreateThread"},
  370. {0x09, SvcWrap<StartThread>, "StartThread"},
  371. {0x0A, SvcWrap<ExitThread>, "ExitThread"},
  372. {0x0B, SvcWrap<SleepThread>, "SleepThread"},
  373. {0x0C, SvcWrap<GetThreadPriority>, "GetThreadPriority"},
  374. {0x0D, SvcWrap<SetThreadPriority>, "SetThreadPriority"},
  375. {0x0E, nullptr, "GetThreadCoreMask"},
  376. {0x0F, nullptr, "SetThreadCoreMask"},
  377. {0x10, SvcWrap<GetCurrentProcessorNumber>, "GetCurrentProcessorNumber"},
  378. {0x11, nullptr, "SignalEvent"},
  379. {0x12, nullptr, "ClearEvent"},
  380. {0x13, nullptr, "MapSharedMemory"},
  381. {0x14, nullptr, "UnmapSharedMemory"},
  382. {0x15, nullptr, "CreateTransferMemory"},
  383. {0x16, SvcWrap<CloseHandle>, "CloseHandle"},
  384. {0x17, nullptr, "ResetSignal"},
  385. {0x18, SvcWrap<WaitSynchronization>, "WaitSynchronization"},
  386. {0x19, nullptr, "CancelSynchronization"},
  387. {0x1A, SvcWrap<LockMutex>, "LockMutex"},
  388. {0x1B, SvcWrap<UnlockMutex>, "UnlockMutex"},
  389. {0x1C, nullptr, "WaitProcessWideKeyAtomic"},
  390. {0x1D, SvcWrap<SignalProcessWideKey>, "SignalProcessWideKey"},
  391. {0x1E, nullptr, "GetSystemTick"},
  392. {0x1F, SvcWrap<ConnectToPort>, "ConnectToPort"},
  393. {0x20, nullptr, "SendSyncRequestLight"},
  394. {0x21, SvcWrap<SendSyncRequest>, "SendSyncRequest"},
  395. {0x22, nullptr, "SendSyncRequestWithUserBuffer"},
  396. {0x23, nullptr, "SendAsyncRequestWithUserBuffer"},
  397. {0x24, SvcWrap<GetProcessId>, "GetProcessId"},
  398. {0x25, SvcWrap<GetThreadId>, "GetThreadId"},
  399. {0x26, SvcWrap<Break>, "Break"},
  400. {0x27, SvcWrap<OutputDebugString>, "OutputDebugString"},
  401. {0x28, nullptr, "ReturnFromException"},
  402. {0x29, SvcWrap<GetInfo>, "GetInfo"},
  403. {0x2A, nullptr, "FlushEntireDataCache"},
  404. {0x2B, nullptr, "FlushDataCache"},
  405. {0x2C, nullptr, "MapPhysicalMemory"},
  406. {0x2D, nullptr, "UnmapPhysicalMemory"},
  407. {0x2E, nullptr, "Unknown"},
  408. {0x2F, nullptr, "GetLastThreadInfo"},
  409. {0x30, nullptr, "GetResourceLimitLimitValue"},
  410. {0x31, nullptr, "GetResourceLimitCurrentValue"},
  411. {0x32, nullptr, "SetThreadActivity"},
  412. {0x33, nullptr, "GetThreadContext"},
  413. {0x34, nullptr, "Unknown"},
  414. {0x35, nullptr, "Unknown"},
  415. {0x36, nullptr, "Unknown"},
  416. {0x37, nullptr, "Unknown"},
  417. {0x38, nullptr, "Unknown"},
  418. {0x39, nullptr, "Unknown"},
  419. {0x3A, nullptr, "Unknown"},
  420. {0x3B, nullptr, "Unknown"},
  421. {0x3C, nullptr, "DumpInfo"},
  422. {0x3D, nullptr, "Unknown"},
  423. {0x3E, nullptr, "Unknown"},
  424. {0x3F, nullptr, "Unknown"},
  425. {0x40, nullptr, "CreateSession"},
  426. {0x41, nullptr, "AcceptSession"},
  427. {0x42, nullptr, "ReplyAndReceiveLight"},
  428. {0x43, nullptr, "ReplyAndReceive"},
  429. {0x44, nullptr, "ReplyAndReceiveWithUserBuffer"},
  430. {0x45, nullptr, "CreateEvent"},
  431. {0x46, nullptr, "Unknown"},
  432. {0x47, nullptr, "Unknown"},
  433. {0x48, nullptr, "Unknown"},
  434. {0x49, nullptr, "Unknown"},
  435. {0x4A, nullptr, "Unknown"},
  436. {0x4B, nullptr, "Unknown"},
  437. {0x4C, nullptr, "Unknown"},
  438. {0x4D, nullptr, "SleepSystem"},
  439. {0x4E, nullptr, "ReadWriteRegister"},
  440. {0x4F, nullptr, "SetProcessActivity"},
  441. {0x50, nullptr, "CreateSharedMemory"},
  442. {0x51, nullptr, "MapTransferMemory"},
  443. {0x52, nullptr, "UnmapTransferMemory"},
  444. {0x53, nullptr, "CreateInterruptEvent"},
  445. {0x54, nullptr, "QueryPhysicalAddress"},
  446. {0x55, nullptr, "QueryIoMapping"},
  447. {0x56, nullptr, "CreateDeviceAddressSpace"},
  448. {0x57, nullptr, "AttachDeviceAddressSpace"},
  449. {0x58, nullptr, "DetachDeviceAddressSpace"},
  450. {0x59, nullptr, "MapDeviceAddressSpaceByForce"},
  451. {0x5A, nullptr, "MapDeviceAddressSpaceAligned"},
  452. {0x5B, nullptr, "MapDeviceAddressSpace"},
  453. {0x5C, nullptr, "UnmapDeviceAddressSpace"},
  454. {0x5D, nullptr, "InvalidateProcessDataCache"},
  455. {0x5E, nullptr, "StoreProcessDataCache"},
  456. {0x5F, nullptr, "FlushProcessDataCache"},
  457. {0x60, nullptr, "DebugActiveProcess"},
  458. {0x61, nullptr, "BreakDebugProcess"},
  459. {0x62, nullptr, "TerminateDebugProcess"},
  460. {0x63, nullptr, "GetDebugEvent"},
  461. {0x64, nullptr, "ContinueDebugEvent"},
  462. {0x65, nullptr, "GetProcessList"},
  463. {0x66, nullptr, "GetThreadList"},
  464. {0x67, nullptr, "GetDebugThreadContext"},
  465. {0x68, nullptr, "SetDebugThreadContext"},
  466. {0x69, nullptr, "QueryDebugProcessMemory"},
  467. {0x6A, nullptr, "ReadDebugProcessMemory"},
  468. {0x6B, nullptr, "WriteDebugProcessMemory"},
  469. {0x6C, nullptr, "SetHardwareBreakPoint"},
  470. {0x6D, nullptr, "GetDebugThreadParam"},
  471. {0x6E, nullptr, "Unknown"},
  472. {0x6F, nullptr, "Unknown"},
  473. {0x70, nullptr, "CreatePort"},
  474. {0x71, nullptr, "ManageNamedPort"},
  475. {0x72, nullptr, "ConnectToPort"},
  476. {0x73, nullptr, "SetProcessMemoryPermission"},
  477. {0x74, nullptr, "MapProcessMemory"},
  478. {0x75, nullptr, "UnmapProcessMemory"},
  479. {0x76, nullptr, "QueryProcessMemory"},
  480. {0x77, nullptr, "MapProcessCodeMemory"},
  481. {0x78, nullptr, "UnmapProcessCodeMemory"},
  482. {0x79, nullptr, "CreateProcess"},
  483. {0x7A, nullptr, "StartProcess"},
  484. {0x7B, nullptr, "TerminateProcess"},
  485. {0x7C, nullptr, "GetProcessInfo"},
  486. {0x7D, nullptr, "CreateResourceLimit"},
  487. {0x7E, nullptr, "SetResourceLimitLimitValue"},
  488. {0x7F, nullptr, "CallSecureMonitor"},
  489. };
  490. static const FunctionDef* GetSVCInfo(u32 func_num) {
  491. if (func_num >= ARRAY_SIZE(SVC_Table)) {
  492. LOG_ERROR(Kernel_SVC, "unknown svc=0x%02X", func_num);
  493. return nullptr;
  494. }
  495. return &SVC_Table[func_num];
  496. }
  497. MICROPROFILE_DEFINE(Kernel_SVC, "Kernel", "SVC", MP_RGB(70, 200, 70));
  498. void CallSVC(u32 immediate) {
  499. MICROPROFILE_SCOPE(Kernel_SVC);
  500. // Lock the global kernel mutex when we enter the kernel HLE.
  501. std::lock_guard<std::recursive_mutex> lock(HLE::g_hle_lock);
  502. const FunctionDef* info = GetSVCInfo(immediate);
  503. if (info) {
  504. if (info->func) {
  505. info->func();
  506. } else {
  507. LOG_CRITICAL(Kernel_SVC, "unimplemented SVC function %s(..)", info->name);
  508. }
  509. } else {
  510. LOG_CRITICAL(Kernel_SVC, "unknown SVC function 0x%x", immediate);
  511. }
  512. }
  513. } // namespace Kernel