svc.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #include <map>
  5. #include <string>
  6. #include "common/string_util.h"
  7. #include "common/symbols.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/kernel.h"
  12. #include "core/hle/kernel/mutex.h"
  13. #include "core/hle/kernel/shared_memory.h"
  14. #include "core/hle/kernel/thread.h"
  15. #include "core/hle/function_wrappers.h"
  16. #include "core/hle/svc.h"
  17. #include "core/hle/service/service.h"
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////
  19. // Namespace SVC
  20. namespace SVC {
  21. enum ControlMemoryOperation {
  22. MEMORY_OPERATION_HEAP = 0x00000003,
  23. MEMORY_OPERATION_GSP_HEAP = 0x00010003,
  24. };
  25. /// Map application or GSP heap memory
  26. Result ControlMemory(u32* out_addr, u32 operation, u32 addr0, u32 addr1, u32 size, u32 permissions) {
  27. DEBUG_LOG(SVC,"called operation=0x%08X, addr0=0x%08X, addr1=0x%08X, size=%08X, permissions=0x%08X",
  28. operation, addr0, addr1, size, permissions);
  29. switch (operation) {
  30. // Map normal heap memory
  31. case MEMORY_OPERATION_HEAP:
  32. *out_addr = Memory::MapBlock_Heap(size, operation, permissions);
  33. break;
  34. // Map GSP heap memory
  35. case MEMORY_OPERATION_GSP_HEAP:
  36. *out_addr = Memory::MapBlock_HeapGSP(size, operation, permissions);
  37. break;
  38. // Unknown ControlMemory operation
  39. default:
  40. ERROR_LOG(SVC, "unknown operation=0x%08X", operation);
  41. }
  42. return 0;
  43. }
  44. /// Maps a memory block to specified address
  45. Result MapMemoryBlock(Handle handle, u32 addr, u32 permissions, u32 other_permissions) {
  46. DEBUG_LOG(SVC, "called memblock=0x%08X, addr=0x%08X, mypermissions=0x%08X, otherpermission=%d",
  47. handle, addr, permissions, other_permissions);
  48. Kernel::MemoryPermission permissions_type = static_cast<Kernel::MemoryPermission>(permissions);
  49. switch (permissions_type) {
  50. case Kernel::MemoryPermission::Read:
  51. case Kernel::MemoryPermission::Write:
  52. case Kernel::MemoryPermission::ReadWrite:
  53. case Kernel::MemoryPermission::DontCare:
  54. Kernel::MapSharedMemory(handle, addr, permissions_type,
  55. static_cast<Kernel::MemoryPermission>(other_permissions));
  56. break;
  57. default:
  58. ERROR_LOG(OSHLE, "unknown permissions=0x%08X", permissions);
  59. }
  60. return 0;
  61. }
  62. /// Connect to an OS service given the port name, returns the handle to the port to out
  63. Result ConnectToPort(Handle* out, const char* port_name) {
  64. Service::Interface* service = Service::g_manager->FetchFromPortName(port_name);
  65. DEBUG_LOG(SVC, "called port_name=%s", port_name);
  66. _assert_msg_(KERNEL, (service != nullptr), "called, but service is not implemented!");
  67. *out = service->GetHandle();
  68. return 0;
  69. }
  70. /// Synchronize to an OS service
  71. Result SendSyncRequest(Handle handle) {
  72. Kernel::Object* object = Kernel::g_object_pool.GetFast<Kernel::Object>(handle);
  73. _assert_msg_(KERNEL, (object != nullptr), "called, but kernel object is nullptr!");
  74. DEBUG_LOG(SVC, "called handle=0x%08X(%s)", handle, object->GetTypeName().c_str());
  75. bool wait = false;
  76. Result res = object->SyncRequest(&wait);
  77. if (wait) {
  78. Kernel::WaitCurrentThread(WAITTYPE_SYNCH); // TODO(bunnei): Is this correct?
  79. }
  80. return res;
  81. }
  82. /// Close a handle
  83. Result CloseHandle(Handle handle) {
  84. // ImplementMe
  85. ERROR_LOG(SVC, "(UNIMPLEMENTED) called handle=0x%08X", handle);
  86. return 0;
  87. }
  88. /// Wait for a handle to synchronize, timeout after the specified nanoseconds
  89. Result WaitSynchronization1(Handle handle, s64 nano_seconds) {
  90. // TODO(bunnei): Do something with nano_seconds, currently ignoring this
  91. bool wait = false;
  92. bool wait_infinite = (nano_seconds == -1); // Used to wait until a thread has terminated
  93. Kernel::Object* object = Kernel::g_object_pool.GetFast<Kernel::Object>(handle);
  94. DEBUG_LOG(SVC, "called handle=0x%08X(%s:%s), nanoseconds=%d", handle, object->GetTypeName().c_str(),
  95. object->GetName().c_str(), nano_seconds);
  96. _assert_msg_(KERNEL, (object != nullptr), "called, but kernel object is nullptr!");
  97. Result res = object->WaitSynchronization(&wait);
  98. // Check for next thread to schedule
  99. if (wait) {
  100. HLE::Reschedule(__func__);
  101. return 0;
  102. }
  103. return res;
  104. }
  105. /// Wait for the given handles to synchronize, timeout after the specified nanoseconds
  106. Result WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count, bool wait_all,
  107. s64 nano_seconds) {
  108. // TODO(bunnei): Do something with nano_seconds, currently ignoring this
  109. bool unlock_all = true;
  110. bool wait_infinite = (nano_seconds == -1); // Used to wait until a thread has terminated
  111. DEBUG_LOG(SVC, "called handle_count=%d, wait_all=%s, nanoseconds=%d",
  112. handle_count, (wait_all ? "true" : "false"), nano_seconds);
  113. // Iterate through each handle, synchronize kernel object
  114. for (s32 i = 0; i < handle_count; i++) {
  115. bool wait = false;
  116. Kernel::Object* object = Kernel::g_object_pool.GetFast<Kernel::Object>(handles[i]);
  117. _assert_msg_(KERNEL, (object != nullptr), "called handle=0x%08X, but kernel object "
  118. "is nullptr!", handles[i]);
  119. DEBUG_LOG(SVC, "\thandle[%d] = 0x%08X(%s:%s)", i, handles[i], object->GetTypeName().c_str(),
  120. object->GetName().c_str());
  121. Result res = object->WaitSynchronization(&wait);
  122. if (!wait && !wait_all) {
  123. *out = i;
  124. return 0;
  125. } else {
  126. unlock_all = false;
  127. }
  128. }
  129. if (wait_all && unlock_all) {
  130. *out = handle_count;
  131. return 0;
  132. }
  133. // Check for next thread to schedule
  134. HLE::Reschedule(__func__);
  135. return 0;
  136. }
  137. /// Create an address arbiter (to allocate access to shared resources)
  138. Result CreateAddressArbiter(u32* arbiter) {
  139. DEBUG_LOG(SVC, "called");
  140. Handle handle = Kernel::CreateAddressArbiter();
  141. *arbiter = handle;
  142. return 0;
  143. }
  144. /// Arbitrate address
  145. Result ArbitrateAddress(Handle arbiter, u32 address, u32 type, u32 value, s64 nanoseconds) {
  146. return Kernel::ArbitrateAddress(arbiter, static_cast<Kernel::ArbitrationType>(type), address,
  147. value);
  148. }
  149. /// Used to output a message on a debug hardware unit - does nothing on a retail unit
  150. void OutputDebugString(const char* string) {
  151. OS_LOG(SVC, "%s", string);
  152. }
  153. /// Get resource limit
  154. Result GetResourceLimit(Handle* resource_limit, Handle process) {
  155. // With regards to proceess values:
  156. // 0xFFFF8001 is a handle alias for the current KProcess, and 0xFFFF8000 is a handle alias for
  157. // the current KThread.
  158. *resource_limit = 0xDEADBEEF;
  159. ERROR_LOG(SVC, "(UNIMPLEMENTED) called process=0x%08X", process);
  160. return 0;
  161. }
  162. /// Get resource limit current values
  163. Result GetResourceLimitCurrentValues(s64* values, Handle resource_limit, void* names,
  164. s32 name_count) {
  165. ERROR_LOG(SVC, "(UNIMPLEMENTED) called resource_limit=%08X, names=%s, name_count=%d",
  166. resource_limit, names, name_count);
  167. Memory::Write32(Core::g_app_core->GetReg(0), 0); // Normmatt: Set used memory to 0 for now
  168. return 0;
  169. }
  170. /// Creates a new thread
  171. Result CreateThread(u32 priority, u32 entry_point, u32 arg, u32 stack_top, u32 processor_id) {
  172. std::string name;
  173. if (Symbols::HasSymbol(entry_point)) {
  174. TSymbol symbol = Symbols::GetSymbol(entry_point);
  175. name = symbol.name;
  176. } else {
  177. name = StringFromFormat("unknown-%08x", entry_point);
  178. }
  179. Handle thread = Kernel::CreateThread(name.c_str(), entry_point, priority, arg, processor_id,
  180. stack_top);
  181. Core::g_app_core->SetReg(1, thread);
  182. DEBUG_LOG(SVC, "called entrypoint=0x%08X (%s), arg=0x%08X, stacktop=0x%08X, "
  183. "threadpriority=0x%08X, processorid=0x%08X : created handle=0x%08X", entry_point,
  184. name.c_str(), arg, stack_top, priority, processor_id, thread);
  185. return 0;
  186. }
  187. /// Called when a thread exits
  188. u32 ExitThread() {
  189. Handle thread = Kernel::GetCurrentThreadHandle();
  190. DEBUG_LOG(SVC, "called, pc=0x%08X", Core::g_app_core->GetPC()); // PC = 0x0010545C
  191. Kernel::StopThread(thread, __func__);
  192. HLE::Reschedule(__func__);
  193. return 0;
  194. }
  195. /// Gets the priority for the specified thread
  196. Result GetThreadPriority(s32* priority, Handle handle) {
  197. *priority = Kernel::GetThreadPriority(handle);
  198. return 0;
  199. }
  200. /// Sets the priority for the specified thread
  201. Result SetThreadPriority(Handle handle, s32 priority) {
  202. return Kernel::SetThreadPriority(handle, priority);
  203. }
  204. /// Create a mutex
  205. Result CreateMutex(Handle* mutex, u32 initial_locked) {
  206. *mutex = Kernel::CreateMutex((initial_locked != 0));
  207. DEBUG_LOG(SVC, "called initial_locked=%s : created handle=0x%08X",
  208. initial_locked ? "true" : "false", *mutex);
  209. return 0;
  210. }
  211. /// Release a mutex
  212. Result ReleaseMutex(Handle handle) {
  213. DEBUG_LOG(SVC, "called handle=0x%08X", handle);
  214. _assert_msg_(KERNEL, (handle != 0), "called, but handle is nullptr!");
  215. Kernel::ReleaseMutex(handle);
  216. return 0;
  217. }
  218. /// Get current thread ID
  219. Result GetThreadId(u32* thread_id, Handle thread) {
  220. ERROR_LOG(SVC, "(UNIMPLEMENTED) called thread=0x%08X", thread);
  221. return 0;
  222. }
  223. /// Query memory
  224. Result QueryMemory(void* info, void* out, u32 addr) {
  225. ERROR_LOG(SVC, "(UNIMPLEMENTED) called addr=0x%08X", addr);
  226. return 0;
  227. }
  228. /// Create an event
  229. Result CreateEvent(Handle* evt, u32 reset_type) {
  230. *evt = Kernel::CreateEvent((ResetType)reset_type);
  231. DEBUG_LOG(SVC, "called reset_type=0x%08X : created handle=0x%08X",
  232. reset_type, *evt);
  233. return 0;
  234. }
  235. /// Duplicates a kernel handle
  236. Result DuplicateHandle(Handle* out, Handle handle) {
  237. DEBUG_LOG(SVC, "called handle=0x%08X", handle);
  238. // Translate kernel handles -> real handles
  239. if (handle == Kernel::CurrentThread) {
  240. handle = Kernel::GetCurrentThreadHandle();
  241. }
  242. _assert_msg_(KERNEL, (handle != Kernel::CurrentProcess),
  243. "(UNIMPLEMENTED) process handle duplication!");
  244. // TODO(bunnei): FixMe - This is a hack to return the handle that we were asked to duplicate.
  245. *out = handle;
  246. return 0;
  247. }
  248. /// Signals an event
  249. Result SignalEvent(Handle evt) {
  250. Result res = Kernel::SignalEvent(evt);
  251. DEBUG_LOG(SVC, "called event=0x%08X", evt);
  252. return res;
  253. }
  254. /// Clears an event
  255. Result ClearEvent(Handle evt) {
  256. Result res = Kernel::ClearEvent(evt);
  257. DEBUG_LOG(SVC, "called event=0x%08X", evt);
  258. return res;
  259. }
  260. /// Sleep the current thread
  261. void SleepThread(s64 nanoseconds) {
  262. DEBUG_LOG(SVC, "called nanoseconds=%d", nanoseconds);
  263. }
  264. /// This returns the total CPU ticks elapsed since the CPU was powered-on
  265. s64 GetSystemTick() {
  266. return (s64)Core::g_app_core->GetTicks();
  267. }
  268. const HLE::FunctionDef SVC_Table[] = {
  269. {0x00, nullptr, "Unknown"},
  270. {0x01, HLE::Wrap<ControlMemory>, "ControlMemory"},
  271. {0x02, HLE::Wrap<QueryMemory>, "QueryMemory"},
  272. {0x03, nullptr, "ExitProcess"},
  273. {0x04, nullptr, "GetProcessAffinityMask"},
  274. {0x05, nullptr, "SetProcessAffinityMask"},
  275. {0x06, nullptr, "GetProcessIdealProcessor"},
  276. {0x07, nullptr, "SetProcessIdealProcessor"},
  277. {0x08, HLE::Wrap<CreateThread>, "CreateThread"},
  278. {0x09, HLE::Wrap<ExitThread>, "ExitThread"},
  279. {0x0A, HLE::Wrap<SleepThread>, "SleepThread"},
  280. {0x0B, HLE::Wrap<GetThreadPriority>, "GetThreadPriority"},
  281. {0x0C, HLE::Wrap<SetThreadPriority>, "SetThreadPriority"},
  282. {0x0D, nullptr, "GetThreadAffinityMask"},
  283. {0x0E, nullptr, "SetThreadAffinityMask"},
  284. {0x0F, nullptr, "GetThreadIdealProcessor"},
  285. {0x10, nullptr, "SetThreadIdealProcessor"},
  286. {0x11, nullptr, "GetCurrentProcessorNumber"},
  287. {0x12, nullptr, "Run"},
  288. {0x13, HLE::Wrap<CreateMutex>, "CreateMutex"},
  289. {0x14, HLE::Wrap<ReleaseMutex>, "ReleaseMutex"},
  290. {0x15, nullptr, "CreateSemaphore"},
  291. {0x16, nullptr, "ReleaseSemaphore"},
  292. {0x17, HLE::Wrap<CreateEvent>, "CreateEvent"},
  293. {0x18, HLE::Wrap<SignalEvent>, "SignalEvent"},
  294. {0x19, HLE::Wrap<ClearEvent>, "ClearEvent"},
  295. {0x1A, nullptr, "CreateTimer"},
  296. {0x1B, nullptr, "SetTimer"},
  297. {0x1C, nullptr, "CancelTimer"},
  298. {0x1D, nullptr, "ClearTimer"},
  299. {0x1E, nullptr, "CreateMemoryBlock"},
  300. {0x1F, HLE::Wrap<MapMemoryBlock>, "MapMemoryBlock"},
  301. {0x20, nullptr, "UnmapMemoryBlock"},
  302. {0x21, HLE::Wrap<CreateAddressArbiter>, "CreateAddressArbiter"},
  303. {0x22, HLE::Wrap<ArbitrateAddress>, "ArbitrateAddress"},
  304. {0x23, HLE::Wrap<CloseHandle>, "CloseHandle"},
  305. {0x24, HLE::Wrap<WaitSynchronization1>, "WaitSynchronization1"},
  306. {0x25, HLE::Wrap<WaitSynchronizationN>, "WaitSynchronizationN"},
  307. {0x26, nullptr, "SignalAndWait"},
  308. {0x27, HLE::Wrap<DuplicateHandle>, "DuplicateHandle"},
  309. {0x28, HLE::Wrap<GetSystemTick>, "GetSystemTick"},
  310. {0x29, nullptr, "GetHandleInfo"},
  311. {0x2A, nullptr, "GetSystemInfo"},
  312. {0x2B, nullptr, "GetProcessInfo"},
  313. {0x2C, nullptr, "GetThreadInfo"},
  314. {0x2D, HLE::Wrap<ConnectToPort>, "ConnectToPort"},
  315. {0x2E, nullptr, "SendSyncRequest1"},
  316. {0x2F, nullptr, "SendSyncRequest2"},
  317. {0x30, nullptr, "SendSyncRequest3"},
  318. {0x31, nullptr, "SendSyncRequest4"},
  319. {0x32, HLE::Wrap<SendSyncRequest>, "SendSyncRequest"},
  320. {0x33, nullptr, "OpenProcess"},
  321. {0x34, nullptr, "OpenThread"},
  322. {0x35, nullptr, "GetProcessId"},
  323. {0x36, nullptr, "GetProcessIdOfThread"},
  324. {0x37, HLE::Wrap<GetThreadId>, "GetThreadId"},
  325. {0x38, HLE::Wrap<GetResourceLimit>, "GetResourceLimit"},
  326. {0x39, nullptr, "GetResourceLimitLimitValues"},
  327. {0x3A, HLE::Wrap<GetResourceLimitCurrentValues>, "GetResourceLimitCurrentValues"},
  328. {0x3B, nullptr, "GetThreadContext"},
  329. {0x3C, nullptr, "Break"},
  330. {0x3D, HLE::Wrap<OutputDebugString>, "OutputDebugString"},
  331. {0x3E, nullptr, "ControlPerformanceCounter"},
  332. {0x3F, nullptr, "Unknown"},
  333. {0x40, nullptr, "Unknown"},
  334. {0x41, nullptr, "Unknown"},
  335. {0x42, nullptr, "Unknown"},
  336. {0x43, nullptr, "Unknown"},
  337. {0x44, nullptr, "Unknown"},
  338. {0x45, nullptr, "Unknown"},
  339. {0x46, nullptr, "Unknown"},
  340. {0x47, nullptr, "CreatePort"},
  341. {0x48, nullptr, "CreateSessionToPort"},
  342. {0x49, nullptr, "CreateSession"},
  343. {0x4A, nullptr, "AcceptSession"},
  344. {0x4B, nullptr, "ReplyAndReceive1"},
  345. {0x4C, nullptr, "ReplyAndReceive2"},
  346. {0x4D, nullptr, "ReplyAndReceive3"},
  347. {0x4E, nullptr, "ReplyAndReceive4"},
  348. {0x4F, nullptr, "ReplyAndReceive"},
  349. {0x50, nullptr, "BindInterrupt"},
  350. {0x51, nullptr, "UnbindInterrupt"},
  351. {0x52, nullptr, "InvalidateProcessDataCache"},
  352. {0x53, nullptr, "StoreProcessDataCache"},
  353. {0x54, nullptr, "FlushProcessDataCache"},
  354. {0x55, nullptr, "StartInterProcessDma"},
  355. {0x56, nullptr, "StopDma"},
  356. {0x57, nullptr, "GetDmaState"},
  357. {0x58, nullptr, "RestartDma"},
  358. {0x59, nullptr, "Unknown"},
  359. {0x5A, nullptr, "Unknown"},
  360. {0x5B, nullptr, "Unknown"},
  361. {0x5C, nullptr, "Unknown"},
  362. {0x5D, nullptr, "Unknown"},
  363. {0x5E, nullptr, "Unknown"},
  364. {0x5F, nullptr, "Unknown"},
  365. {0x60, nullptr, "DebugActiveProcess"},
  366. {0x61, nullptr, "BreakDebugProcess"},
  367. {0x62, nullptr, "TerminateDebugProcess"},
  368. {0x63, nullptr, "GetProcessDebugEvent"},
  369. {0x64, nullptr, "ContinueDebugEvent"},
  370. {0x65, nullptr, "GetProcessList"},
  371. {0x66, nullptr, "GetThreadList"},
  372. {0x67, nullptr, "GetDebugThreadContext"},
  373. {0x68, nullptr, "SetDebugThreadContext"},
  374. {0x69, nullptr, "QueryDebugProcessMemory"},
  375. {0x6A, nullptr, "ReadProcessMemory"},
  376. {0x6B, nullptr, "WriteProcessMemory"},
  377. {0x6C, nullptr, "SetHardwareBreakPoint"},
  378. {0x6D, nullptr, "GetDebugThreadParam"},
  379. {0x6E, nullptr, "Unknown"},
  380. {0x6F, nullptr, "Unknown"},
  381. {0x70, nullptr, "ControlProcessMemory"},
  382. {0x71, nullptr, "MapProcessMemory"},
  383. {0x72, nullptr, "UnmapProcessMemory"},
  384. {0x73, nullptr, "Unknown"},
  385. {0x74, nullptr, "Unknown"},
  386. {0x75, nullptr, "Unknown"},
  387. {0x76, nullptr, "TerminateProcess"},
  388. {0x77, nullptr, "Unknown"},
  389. {0x78, nullptr, "CreateResourceLimit"},
  390. {0x79, nullptr, "Unknown"},
  391. {0x7A, nullptr, "Unknown"},
  392. {0x7B, nullptr, "Unknown"},
  393. {0x7C, nullptr, "KernelSetState"},
  394. {0x7D, nullptr, "QueryProcessMemory"},
  395. };
  396. void Register() {
  397. HLE::RegisterModule("SVC_Table", ARRAY_SIZE(SVC_Table), SVC_Table);
  398. }
  399. } // namespace