svc.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 != 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());
  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(),
  95. object->GetName(), 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(void* _out, void* _handles, u32 handle_count, u32 wait_all,
  107. s64 nano_seconds) {
  108. // TODO(bunnei): Do something with nano_seconds, currently ignoring this
  109. s32* out = (s32*)_out;
  110. Handle* handles = (Handle*)_handles;
  111. bool unlock_all = true;
  112. bool wait_infinite = (nano_seconds == -1); // Used to wait until a thread has terminated
  113. DEBUG_LOG(SVC, "called handle_count=%d, wait_all=%s, nanoseconds=%d",
  114. handle_count, (wait_all ? "true" : "false"), nano_seconds);
  115. // Iterate through each handle, synchronize kernel object
  116. for (u32 i = 0; i < handle_count; i++) {
  117. bool wait = false;
  118. Kernel::Object* object = Kernel::g_object_pool.GetFast<Kernel::Object>(handles[i]);
  119. _assert_msg_(KERNEL, (object != nullptr), "called handle=0x%08X, but kernel object "
  120. "is nullptr!", handles[i]);
  121. DEBUG_LOG(SVC, "\thandle[%d] = 0x%08X(%s:%s)", i, handles[i], object->GetTypeName(),
  122. object->GetName());
  123. Result res = object->WaitSynchronization(&wait);
  124. if (!wait && !wait_all) {
  125. *out = i;
  126. return 0;
  127. } else {
  128. unlock_all = false;
  129. }
  130. }
  131. if (wait_all && unlock_all) {
  132. *out = handle_count;
  133. return 0;
  134. }
  135. // Check for next thread to schedule
  136. HLE::Reschedule(__func__);
  137. return 0;
  138. }
  139. /// Create an address arbiter (to allocate access to shared resources)
  140. Result CreateAddressArbiter(void* arbiter) {
  141. ERROR_LOG(SVC, "(UNIMPLEMENTED) called");
  142. Core::g_app_core->SetReg(1, 0xFABBDADD);
  143. return 0;
  144. }
  145. /// Arbitrate address
  146. Result ArbitrateAddress(Handle arbiter, u32 addr, u32 _type, u32 value, s64 nanoseconds) {
  147. ERROR_LOG(SVC, "(UNIMPLEMENTED) called");
  148. ArbitrationType type = (ArbitrationType)_type;
  149. Memory::Write32(addr, type);
  150. return 0;
  151. }
  152. /// Used to output a message on a debug hardware unit - does nothing on a retail unit
  153. void OutputDebugString(const char* string) {
  154. OS_LOG(SVC, "%s", string);
  155. }
  156. /// Get resource limit
  157. Result GetResourceLimit(void* _resource_limit, Handle process) {
  158. // With regards to proceess values:
  159. // 0xFFFF8001 is a handle alias for the current KProcess, and 0xFFFF8000 is a handle alias for
  160. // the current KThread.
  161. Handle* resource_limit = (Handle*)_resource_limit;
  162. *resource_limit = 0xDEADBEEF;
  163. ERROR_LOG(SVC, "(UNIMPLEMENTED) called process=0x%08X", process);
  164. return 0;
  165. }
  166. /// Get resource limit current values
  167. Result GetResourceLimitCurrentValues(void* _values, Handle resource_limit, void* names,
  168. s32 name_count) {
  169. ERROR_LOG(SVC, "(UNIMPLEMENTED) called resource_limit=%08X, names=%s, name_count=%d",
  170. resource_limit, names, name_count);
  171. Memory::Write32(Core::g_app_core->GetReg(0), 0); // Normmatt: Set used memory to 0 for now
  172. return 0;
  173. }
  174. /// Creates a new thread
  175. Result CreateThread(u32 priority, u32 entry_point, u32 arg, u32 stack_top, u32 processor_id) {
  176. std::string name;
  177. if (Symbols::HasSymbol(entry_point)) {
  178. TSymbol symbol = Symbols::GetSymbol(entry_point);
  179. name = symbol.name;
  180. } else {
  181. char buff[100];
  182. sprintf(buff, "%s", "unknown-%08X", entry_point);
  183. name = buff;
  184. }
  185. Handle thread = Kernel::CreateThread(name.c_str(), entry_point, priority, arg, processor_id,
  186. stack_top);
  187. Core::g_app_core->SetReg(1, thread);
  188. DEBUG_LOG(SVC, "called entrypoint=0x%08X (%s), arg=0x%08X, stacktop=0x%08X, "
  189. "threadpriority=0x%08X, processorid=0x%08X : created handle=0x%08X", entry_point,
  190. name.c_str(), arg, stack_top, priority, processor_id, thread);
  191. return 0;
  192. }
  193. /// Called when a thread exits
  194. u32 ExitThread() {
  195. Handle thread = Kernel::GetCurrentThreadHandle();
  196. DEBUG_LOG(SVC, "called, pc=0x%08X", Core::g_app_core->GetPC()); // PC = 0x0010545C
  197. Kernel::StopThread(thread, __func__);
  198. HLE::Reschedule(__func__);
  199. return 0;
  200. }
  201. /// Gets the priority for the specified thread
  202. Result GetThreadPriority(void* _priority, Handle handle) {
  203. s32* priority = (s32*)_priority;
  204. *priority = Kernel::GetThreadPriority(handle);
  205. return 0;
  206. }
  207. /// Sets the priority for the specified thread
  208. Result SetThreadPriority(Handle handle, s32 priority) {
  209. return Kernel::SetThreadPriority(handle, priority);
  210. }
  211. /// Create a mutex
  212. Result CreateMutex(void* _mutex, u32 initial_locked) {
  213. Handle* mutex = (Handle*)_mutex;
  214. *mutex = Kernel::CreateMutex((initial_locked != 0));
  215. DEBUG_LOG(SVC, "called initial_locked=%s : created handle=0x%08X",
  216. initial_locked ? "true" : "false", *mutex);
  217. return 0;
  218. }
  219. /// Release a mutex
  220. Result ReleaseMutex(Handle handle) {
  221. DEBUG_LOG(SVC, "called handle=0x%08X", handle);
  222. _assert_msg_(KERNEL, (handle != 0), "called, but handle is nullptr!");
  223. Kernel::ReleaseMutex(handle);
  224. return 0;
  225. }
  226. /// Get current thread ID
  227. Result GetThreadId(void* thread_id, u32 thread) {
  228. ERROR_LOG(SVC, "(UNIMPLEMENTED) called thread=0x%08X", thread);
  229. return 0;
  230. }
  231. /// Query memory
  232. Result QueryMemory(void *_info, void *_out, u32 addr) {
  233. ERROR_LOG(SVC, "(UNIMPLEMENTED) called addr=0x%08X", addr);
  234. return 0;
  235. }
  236. /// Create an event
  237. Result CreateEvent(void* _event, u32 reset_type) {
  238. Handle* evt = (Handle*)_event;
  239. *evt = Kernel::CreateEvent((ResetType)reset_type);
  240. DEBUG_LOG(SVC, "called reset_type=0x%08X : created handle=0x%08X",
  241. reset_type, *evt);
  242. return 0;
  243. }
  244. /// Duplicates a kernel handle
  245. Result DuplicateHandle(void* _out, Handle handle) {
  246. Handle* out = (Handle*)_out;
  247. DEBUG_LOG(SVC, "called handle=0x%08X", handle);
  248. // Translate kernel handles -> real handles
  249. if (handle == Kernel::CurrentThread) {
  250. handle = Kernel::GetCurrentThreadHandle();
  251. }
  252. _assert_msg_(KERNEL, (handle != Kernel::CurrentProcess),
  253. "(UNIMPLEMENTED) process handle duplication!");
  254. // TODO(bunnei): FixMe - This is a hack to return the handle that we were asked to duplicate.
  255. *out = handle;
  256. return 0;
  257. }
  258. /// Signals an event
  259. Result SignalEvent(Handle evt) {
  260. Result res = Kernel::SignalEvent(evt);
  261. DEBUG_LOG(SVC, "called event=0x%08X", evt);
  262. return res;
  263. }
  264. /// Clears an event
  265. Result ClearEvent(Handle evt) {
  266. Result res = Kernel::ClearEvent(evt);
  267. DEBUG_LOG(SVC, "called event=0x%08X", evt);
  268. return res;
  269. }
  270. /// Sleep the current thread
  271. void SleepThread(s64 nanoseconds) {
  272. DEBUG_LOG(SVC, "called nanoseconds=%d", nanoseconds);
  273. }
  274. const HLE::FunctionDef SVC_Table[] = {
  275. {0x00, nullptr, "Unknown"},
  276. {0x01, WrapI_VUUUUU<ControlMemory>, "ControlMemory"},
  277. {0x02, WrapI_VVU<QueryMemory>, "QueryMemory"},
  278. {0x03, nullptr, "ExitProcess"},
  279. {0x04, nullptr, "GetProcessAffinityMask"},
  280. {0x05, nullptr, "SetProcessAffinityMask"},
  281. {0x06, nullptr, "GetProcessIdealProcessor"},
  282. {0x07, nullptr, "SetProcessIdealProcessor"},
  283. {0x08, WrapI_UUUUU<CreateThread>, "CreateThread"},
  284. {0x09, WrapU_V<ExitThread>, "ExitThread"},
  285. {0x0A, WrapV_S64<SleepThread>, "SleepThread"},
  286. {0x0B, WrapI_VU<GetThreadPriority>, "GetThreadPriority"},
  287. {0x0C, WrapI_UI<SetThreadPriority>, "SetThreadPriority"},
  288. {0x0D, nullptr, "GetThreadAffinityMask"},
  289. {0x0E, nullptr, "SetThreadAffinityMask"},
  290. {0x0F, nullptr, "GetThreadIdealProcessor"},
  291. {0x10, nullptr, "SetThreadIdealProcessor"},
  292. {0x11, nullptr, "GetCurrentProcessorNumber"},
  293. {0x12, nullptr, "Run"},
  294. {0x13, WrapI_VU<CreateMutex>, "CreateMutex"},
  295. {0x14, WrapI_U<ReleaseMutex>, "ReleaseMutex"},
  296. {0x15, nullptr, "CreateSemaphore"},
  297. {0x16, nullptr, "ReleaseSemaphore"},
  298. {0x17, WrapI_VU<CreateEvent>, "CreateEvent"},
  299. {0x18, WrapI_U<SignalEvent>, "SignalEvent"},
  300. {0x19, WrapI_U<ClearEvent>, "ClearEvent"},
  301. {0x1A, nullptr, "CreateTimer"},
  302. {0x1B, nullptr, "SetTimer"},
  303. {0x1C, nullptr, "CancelTimer"},
  304. {0x1D, nullptr, "ClearTimer"},
  305. {0x1E, nullptr, "CreateMemoryBlock"},
  306. {0x1F, WrapI_UUUU<MapMemoryBlock>, "MapMemoryBlock"},
  307. {0x20, nullptr, "UnmapMemoryBlock"},
  308. {0x21, WrapI_V<CreateAddressArbiter>, "CreateAddressArbiter"},
  309. {0x22, WrapI_UUUUS64<ArbitrateAddress>, "ArbitrateAddress"},
  310. {0x23, WrapI_U<CloseHandle>, "CloseHandle"},
  311. {0x24, WrapI_US64<WaitSynchronization1>, "WaitSynchronization1"},
  312. {0x25, WrapI_VVUUS64<WaitSynchronizationN>, "WaitSynchronizationN"},
  313. {0x26, nullptr, "SignalAndWait"},
  314. {0x27, WrapI_VU<DuplicateHandle>, "DuplicateHandle"},
  315. {0x28, nullptr, "GetSystemTick"},
  316. {0x29, nullptr, "GetHandleInfo"},
  317. {0x2A, nullptr, "GetSystemInfo"},
  318. {0x2B, nullptr, "GetProcessInfo"},
  319. {0x2C, nullptr, "GetThreadInfo"},
  320. {0x2D, WrapI_VC<ConnectToPort>, "ConnectToPort"},
  321. {0x2E, nullptr, "SendSyncRequest1"},
  322. {0x2F, nullptr, "SendSyncRequest2"},
  323. {0x30, nullptr, "SendSyncRequest3"},
  324. {0x31, nullptr, "SendSyncRequest4"},
  325. {0x32, WrapI_U<SendSyncRequest>, "SendSyncRequest"},
  326. {0x33, nullptr, "OpenProcess"},
  327. {0x34, nullptr, "OpenThread"},
  328. {0x35, nullptr, "GetProcessId"},
  329. {0x36, nullptr, "GetProcessIdOfThread"},
  330. {0x37, WrapI_VU<GetThreadId>, "GetThreadId"},
  331. {0x38, WrapI_VU<GetResourceLimit>, "GetResourceLimit"},
  332. {0x39, nullptr, "GetResourceLimitLimitValues"},
  333. {0x3A, WrapI_VUVI<GetResourceLimitCurrentValues>, "GetResourceLimitCurrentValues"},
  334. {0x3B, nullptr, "GetThreadContext"},
  335. {0x3C, nullptr, "Break"},
  336. {0x3D, WrapV_C<OutputDebugString>, "OutputDebugString"},
  337. {0x3E, nullptr, "ControlPerformanceCounter"},
  338. {0x3F, nullptr, "Unknown"},
  339. {0x40, nullptr, "Unknown"},
  340. {0x41, nullptr, "Unknown"},
  341. {0x42, nullptr, "Unknown"},
  342. {0x43, nullptr, "Unknown"},
  343. {0x44, nullptr, "Unknown"},
  344. {0x45, nullptr, "Unknown"},
  345. {0x46, nullptr, "Unknown"},
  346. {0x47, nullptr, "CreatePort"},
  347. {0x48, nullptr, "CreateSessionToPort"},
  348. {0x49, nullptr, "CreateSession"},
  349. {0x4A, nullptr, "AcceptSession"},
  350. {0x4B, nullptr, "ReplyAndReceive1"},
  351. {0x4C, nullptr, "ReplyAndReceive2"},
  352. {0x4D, nullptr, "ReplyAndReceive3"},
  353. {0x4E, nullptr, "ReplyAndReceive4"},
  354. {0x4F, nullptr, "ReplyAndReceive"},
  355. {0x50, nullptr, "BindInterrupt"},
  356. {0x51, nullptr, "UnbindInterrupt"},
  357. {0x52, nullptr, "InvalidateProcessDataCache"},
  358. {0x53, nullptr, "StoreProcessDataCache"},
  359. {0x54, nullptr, "FlushProcessDataCache"},
  360. {0x55, nullptr, "StartInterProcessDma"},
  361. {0x56, nullptr, "StopDma"},
  362. {0x57, nullptr, "GetDmaState"},
  363. {0x58, nullptr, "RestartDma"},
  364. {0x59, nullptr, "Unknown"},
  365. {0x5A, nullptr, "Unknown"},
  366. {0x5B, nullptr, "Unknown"},
  367. {0x5C, nullptr, "Unknown"},
  368. {0x5D, nullptr, "Unknown"},
  369. {0x5E, nullptr, "Unknown"},
  370. {0x5F, nullptr, "Unknown"},
  371. {0x60, nullptr, "DebugActiveProcess"},
  372. {0x61, nullptr, "BreakDebugProcess"},
  373. {0x62, nullptr, "TerminateDebugProcess"},
  374. {0x63, nullptr, "GetProcessDebugEvent"},
  375. {0x64, nullptr, "ContinueDebugEvent"},
  376. {0x65, nullptr, "GetProcessList"},
  377. {0x66, nullptr, "GetThreadList"},
  378. {0x67, nullptr, "GetDebugThreadContext"},
  379. {0x68, nullptr, "SetDebugThreadContext"},
  380. {0x69, nullptr, "QueryDebugProcessMemory"},
  381. {0x6A, nullptr, "ReadProcessMemory"},
  382. {0x6B, nullptr, "WriteProcessMemory"},
  383. {0x6C, nullptr, "SetHardwareBreakPoint"},
  384. {0x6D, nullptr, "GetDebugThreadParam"},
  385. {0x6E, nullptr, "Unknown"},
  386. {0x6F, nullptr, "Unknown"},
  387. {0x70, nullptr, "ControlProcessMemory"},
  388. {0x71, nullptr, "MapProcessMemory"},
  389. {0x72, nullptr, "UnmapProcessMemory"},
  390. {0x73, nullptr, "Unknown"},
  391. {0x74, nullptr, "Unknown"},
  392. {0x75, nullptr, "Unknown"},
  393. {0x76, nullptr, "TerminateProcess"},
  394. {0x77, nullptr, "Unknown"},
  395. {0x78, nullptr, "CreateResourceLimit"},
  396. {0x79, nullptr, "Unknown"},
  397. {0x7A, nullptr, "Unknown"},
  398. {0x7B, nullptr, "Unknown"},
  399. {0x7C, nullptr, "KernelSetState"},
  400. {0x7D, nullptr, "QueryProcessMemory"},
  401. };
  402. void Register() {
  403. HLE::RegisterModule("SVC_Table", ARRAY_SIZE(SVC_Table), SVC_Table);
  404. }
  405. } // namespace