svc.cpp 20 KB

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