svc.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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/kernel.h"
  9. #include "core/hle/kernel/thread.h"
  10. #include "core/hle/function_wrappers.h"
  11. #include "core/hle/svc.h"
  12. #include "core/hle/service/service.h"
  13. #include "core/hle/kernel/thread.h"
  14. ////////////////////////////////////////////////////////////////////////////////////////////////////
  15. // Namespace SVC
  16. namespace SVC {
  17. enum ControlMemoryOperation {
  18. MEMORY_OPERATION_HEAP = 0x00000003,
  19. MEMORY_OPERATION_GSP_HEAP = 0x00010003,
  20. };
  21. enum MapMemoryPermission {
  22. MEMORY_PERMISSION_UNMAP = 0x00000000,
  23. MEMORY_PERMISSION_NORMAL = 0x00000001,
  24. };
  25. /// Map application or GSP heap memory
  26. Result ControlMemory(void* _outaddr, u32 operation, u32 addr0, u32 addr1, u32 size, u32 permissions) {
  27. u32* outaddr = (u32*)_outaddr;
  28. u32 virtual_address = 0x00000000;
  29. DEBUG_LOG(SVC, "ControlMemory 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. virtual_address = Memory::MapBlock_Heap(size, operation, permissions);
  35. break;
  36. // Map GSP heap memory
  37. case MEMORY_OPERATION_GSP_HEAP:
  38. virtual_address = Memory::MapBlock_HeapGSP(size, operation, permissions);
  39. break;
  40. // Unknown ControlMemory operation
  41. default:
  42. ERROR_LOG(SVC, "ControlMemory unknown operation=0x%08X", operation);
  43. }
  44. if (NULL != outaddr) {
  45. *outaddr = virtual_address;
  46. }
  47. Core::g_app_core->SetReg(1, virtual_address);
  48. return 0;
  49. }
  50. /// Maps a memory block to specified address
  51. Result MapMemoryBlock(Handle memblock, u32 addr, u32 mypermissions, u32 otherpermission) {
  52. DEBUG_LOG(SVC, "MapMemoryBlock called memblock=0x08X, addr=0x%08X, mypermissions=0x%08X, otherpermission=%d",
  53. memblock, addr, mypermissions, otherpermission);
  54. switch (mypermissions) {
  55. case MEMORY_PERMISSION_NORMAL:
  56. case MEMORY_PERMISSION_NORMAL + 1:
  57. case MEMORY_PERMISSION_NORMAL + 2:
  58. Memory::MapBlock_Shared(memblock, addr, mypermissions);
  59. break;
  60. default:
  61. ERROR_LOG(OSHLE, "MapMemoryBlock unknown permissions=0x%08X", mypermissions);
  62. }
  63. return 0;
  64. }
  65. /// Connect to an OS service given the port name, returns the handle to the port to out
  66. Result ConnectToPort(void* out, const char* port_name) {
  67. Service::Interface* service = Service::g_manager->FetchFromPortName(port_name);
  68. Core::g_app_core->SetReg(1, service->GetHandle());
  69. DEBUG_LOG(SVC, "ConnectToPort called port_name=%s", port_name);
  70. return 0;
  71. }
  72. /// Synchronize to an OS service
  73. Result SendSyncRequest(Handle handle) {
  74. DEBUG_LOG(SVC, "SendSyncRequest called handle=0x%08X");
  75. Service::Interface* service = Service::g_manager->FetchFromHandle(handle);
  76. service->Sync();
  77. return 0;
  78. }
  79. /// Close a handle
  80. Result CloseHandle(Handle handle) {
  81. // ImplementMe
  82. DEBUG_LOG(SVC, "(UNIMPLEMENTED) CloseHandle called handle=0x%08X", handle);
  83. return 0;
  84. }
  85. /// Wait for a handle to synchronize, timeout after the specified nanoseconds
  86. Result WaitSynchronization1(Handle handle, s64 nano_seconds) {
  87. // ImplementMe
  88. DEBUG_LOG(SVC, "(UNIMPLEMENTED) WaitSynchronization1 called handle=0x%08X, nanoseconds=%d",
  89. handle, nano_seconds);
  90. return 0;
  91. }
  92. /// Wait for the given handles to synchronize, timeout after the specified nanoseconds
  93. Result WaitSynchronizationN(void* _out, void* _handles, u32 handle_count, u32 wait_all, s64 nano_seconds) {
  94. s32* out = (s32*)_out;
  95. Handle* handles = (Handle*)_handles;
  96. // ImplementMe
  97. DEBUG_LOG(SVC, "(UNIMPLEMENTED) WaitSynchronizationN called handle_count=%d, wait_all=%s, nanoseconds=%d %s",
  98. handle_count, (wait_all ? "true" : "false"), nano_seconds);
  99. for (u32 i = 0; i < handle_count; i++) {
  100. DEBUG_LOG(SVC, "\thandle[%d]=0x%08X", i, handles[i]);
  101. }
  102. __KernelReschedule("WaitSynchronizationN");
  103. return 0;
  104. }
  105. /// Create an address arbiter (to allocate access to shared resources)
  106. Result CreateAddressArbiter(void* arbiter) {
  107. // ImplementMe
  108. DEBUG_LOG(SVC, "(UNIMPLEMENTED) CreateAddressArbiter called");
  109. Core::g_app_core->SetReg(1, 0xFABBDADD);
  110. return 0;
  111. }
  112. /// Used to output a message on a debug hardware unit - does nothing on a retail unit
  113. void OutputDebugString(const char* string) {
  114. NOTICE_LOG(SVC, "## OSDEBUG: %08X %s", Core::g_app_core->GetPC(), string);
  115. }
  116. /// Get resource limit
  117. Result GetResourceLimit(void* resource_limit, Handle process) {
  118. // With regards to proceess values:
  119. // 0xFFFF8001 is a handle alias for the current KProcess, and 0xFFFF8000 is a handle alias for
  120. // the current KThread.
  121. DEBUG_LOG(SVC, "(UNIMPLEMENTED) GetResourceLimit called process=0x%08X", process);
  122. Core::g_app_core->SetReg(1, 0xDEADBEEF);
  123. return 0;
  124. }
  125. /// Get resource limit current values
  126. Result GetResourceLimitCurrentValues(void* _values, Handle resource_limit, void* names, s32 name_count) {
  127. //s64* values = (s64*)_values;
  128. DEBUG_LOG(SVC, "(UNIMPLEMENTED) GetResourceLimitCurrentValues called resource_limit=%08X, names=%s, name_count=%d",
  129. resource_limit, names, name_count);
  130. Memory::Write32(Core::g_app_core->GetReg(0), 0); // Normmatt: Set used memory to 0 for now
  131. return 0;
  132. }
  133. Result CreateThread(void* thread, u32 priority, u32 entry_point, u32 arg, u32 stack_top,
  134. u32 processor_id) {
  135. std::string name;
  136. if (Symbols::HasSymbol(entry_point)) {
  137. TSymbol symbol = Symbols::GetSymbol(entry_point);
  138. name = symbol.name;
  139. } else {
  140. char buff[100];
  141. sprintf(buff, "%s", "unknown-%08X", entry_point);
  142. name = buff;
  143. }
  144. DEBUG_LOG(SVC, "CreateThread called entrypoint=0x%08X (%s), arg=0x%08X, stacktop=0x%08X, "
  145. "threadpriority=0x%08X, processorid=0x%08X", entry_point, name.c_str(), arg, stack_top,
  146. priority, processor_id);
  147. Handle handle = __KernelCreateThread(name.c_str(), entry_point, priority, processor_id,
  148. stack_top);
  149. Core::g_app_core->SetReg(1, 0xFEEDDEAF);
  150. return 0;
  151. }
  152. Result CreateMutex(void* _mutex, u32 initial_locked) {
  153. Handle* mutex = (Handle*)_mutex;
  154. DEBUG_LOG(SVC, "(UNIMPLEMENTED) CreateMutex called initial_locked=%s",
  155. initial_locked ? "true" : "false");
  156. Core::g_app_core->SetReg(1, 0xF00D0BAD);
  157. return 0;
  158. }
  159. Result ReleaseMutex(Handle handle) {
  160. DEBUG_LOG(SVC, "(UNIMPLEMENTED) ReleaseMutex called handle=0x%08X", handle);
  161. return 0;
  162. }
  163. Result GetThreadId(void* thread_id, u32 thread) {
  164. DEBUG_LOG(SVC, "(UNIMPLEMENTED) GetThreadId called thread=0x%08X", thread);
  165. return 0;
  166. }
  167. Result QueryMemory(void *_info, void *_out, u32 addr) {
  168. MemoryInfo* info = (MemoryInfo*) _info;
  169. PageInfo* out = (PageInfo*) _out;
  170. DEBUG_LOG(SVC, "(UNIMPLEMENTED) QueryMemory called addr=0x%08X", addr);
  171. return 0;
  172. }
  173. Result CreateEvent(void* _event, u32 reset_type) {
  174. Handle* event = (Handle*)_event;
  175. DEBUG_LOG(SVC, "(UNIMPLEMENTED) CreateEvent called reset_type=0x%08X", reset_type);
  176. Core::g_app_core->SetReg(1, 0xBADC0DE0);
  177. return 0;
  178. }
  179. const HLE::FunctionDef SVC_Table[] = {
  180. {0x00, NULL, "Unknown"},
  181. {0x01, WrapI_VUUUUU<ControlMemory>, "ControlMemory"},
  182. {0x02, WrapI_VVU<QueryMemory>, "QueryMemory"},
  183. {0x03, NULL, "ExitProcess"},
  184. {0x04, NULL, "GetProcessAffinityMask"},
  185. {0x05, NULL, "SetProcessAffinityMask"},
  186. {0x06, NULL, "GetProcessIdealProcessor"},
  187. {0x07, NULL, "SetProcessIdealProcessor"},
  188. {0x08, WrapI_VUUUUU<CreateThread>, "CreateThread"},
  189. {0x09, NULL, "ExitThread"},
  190. {0x0A, NULL, "SleepThread"},
  191. {0x0B, NULL, "GetThreadPriority"},
  192. {0x0C, NULL, "SetThreadPriority"},
  193. {0x0D, NULL, "GetThreadAffinityMask"},
  194. {0x0E, NULL, "SetThreadAffinityMask"},
  195. {0x0F, NULL, "GetThreadIdealProcessor"},
  196. {0x10, NULL, "SetThreadIdealProcessor"},
  197. {0x11, NULL, "GetCurrentProcessorNumber"},
  198. {0x12, NULL, "Run"},
  199. {0x13, WrapI_VU<CreateMutex>, "CreateMutex"},
  200. {0x14, WrapI_U<ReleaseMutex>, "ReleaseMutex"},
  201. {0x15, NULL, "CreateSemaphore"},
  202. {0x16, NULL, "ReleaseSemaphore"},
  203. {0x17, WrapI_VU<CreateEvent>, "CreateEvent"},
  204. {0x18, NULL, "SignalEvent"},
  205. {0x19, NULL, "ClearEvent"},
  206. {0x1A, NULL, "CreateTimer"},
  207. {0x1B, NULL, "SetTimer"},
  208. {0x1C, NULL, "CancelTimer"},
  209. {0x1D, NULL, "ClearTimer"},
  210. {0x1E, NULL, "CreateMemoryBlock"},
  211. {0x1F, WrapI_UUUU<MapMemoryBlock>, "MapMemoryBlock"},
  212. {0x20, NULL, "UnmapMemoryBlock"},
  213. {0x21, WrapI_V<CreateAddressArbiter>, "CreateAddressArbiter"},
  214. {0x22, NULL, "ArbitrateAddress"},
  215. {0x23, WrapI_U<CloseHandle>, "CloseHandle"},
  216. {0x24, WrapI_US64<WaitSynchronization1>, "WaitSynchronization1"},
  217. {0x25, WrapI_VVUUS64<WaitSynchronizationN>, "WaitSynchronizationN"},
  218. {0x26, NULL, "SignalAndWait"},
  219. {0x27, NULL, "DuplicateHandle"},
  220. {0x28, NULL, "GetSystemTick"},
  221. {0x29, NULL, "GetHandleInfo"},
  222. {0x2A, NULL, "GetSystemInfo"},
  223. {0x2B, NULL, "GetProcessInfo"},
  224. {0x2C, NULL, "GetThreadInfo"},
  225. {0x2D, WrapI_VC<ConnectToPort>, "ConnectToPort"},
  226. {0x2E, NULL, "SendSyncRequest1"},
  227. {0x2F, NULL, "SendSyncRequest2"},
  228. {0x30, NULL, "SendSyncRequest3"},
  229. {0x31, NULL, "SendSyncRequest4"},
  230. {0x32, WrapI_U<SendSyncRequest>, "SendSyncRequest"},
  231. {0x33, NULL, "OpenProcess"},
  232. {0x34, NULL, "OpenThread"},
  233. {0x35, NULL, "GetProcessId"},
  234. {0x36, NULL, "GetProcessIdOfThread"},
  235. {0x37, WrapI_VU<GetThreadId>, "GetThreadId"},
  236. {0x38, WrapI_VU<GetResourceLimit>, "GetResourceLimit"},
  237. {0x39, NULL, "GetResourceLimitLimitValues"},
  238. {0x3A, WrapI_VUVI<GetResourceLimitCurrentValues>, "GetResourceLimitCurrentValues"},
  239. {0x3B, NULL, "GetThreadContext"},
  240. {0x3C, NULL, "Break"},
  241. {0x3D, WrapV_C<OutputDebugString>, "OutputDebugString"},
  242. {0x3E, NULL, "ControlPerformanceCounter"},
  243. {0x3F, NULL, "Unknown"},
  244. {0x40, NULL, "Unknown"},
  245. {0x41, NULL, "Unknown"},
  246. {0x42, NULL, "Unknown"},
  247. {0x43, NULL, "Unknown"},
  248. {0x44, NULL, "Unknown"},
  249. {0x45, NULL, "Unknown"},
  250. {0x46, NULL, "Unknown"},
  251. {0x47, NULL, "CreatePort"},
  252. {0x48, NULL, "CreateSessionToPort"},
  253. {0x49, NULL, "CreateSession"},
  254. {0x4A, NULL, "AcceptSession"},
  255. {0x4B, NULL, "ReplyAndReceive1"},
  256. {0x4C, NULL, "ReplyAndReceive2"},
  257. {0x4D, NULL, "ReplyAndReceive3"},
  258. {0x4E, NULL, "ReplyAndReceive4"},
  259. {0x4F, NULL, "ReplyAndReceive"},
  260. {0x50, NULL, "BindInterrupt"},
  261. {0x51, NULL, "UnbindInterrupt"},
  262. {0x52, NULL, "InvalidateProcessDataCache"},
  263. {0x53, NULL, "StoreProcessDataCache"},
  264. {0x54, NULL, "FlushProcessDataCache"},
  265. {0x55, NULL, "StartInterProcessDma"},
  266. {0x56, NULL, "StopDma"},
  267. {0x57, NULL, "GetDmaState"},
  268. {0x58, NULL, "RestartDma"},
  269. {0x59, NULL, "Unknown"},
  270. {0x5A, NULL, "Unknown"},
  271. {0x5B, NULL, "Unknown"},
  272. {0x5C, NULL, "Unknown"},
  273. {0x5D, NULL, "Unknown"},
  274. {0x5E, NULL, "Unknown"},
  275. {0x5F, NULL, "Unknown"},
  276. {0x60, NULL, "DebugActiveProcess"},
  277. {0x61, NULL, "BreakDebugProcess"},
  278. {0x62, NULL, "TerminateDebugProcess"},
  279. {0x63, NULL, "GetProcessDebugEvent"},
  280. {0x64, NULL, "ContinueDebugEvent"},
  281. {0x65, NULL, "GetProcessList"},
  282. {0x66, NULL, "GetThreadList"},
  283. {0x67, NULL, "GetDebugThreadContext"},
  284. {0x68, NULL, "SetDebugThreadContext"},
  285. {0x69, NULL, "QueryDebugProcessMemory"},
  286. {0x6A, NULL, "ReadProcessMemory"},
  287. {0x6B, NULL, "WriteProcessMemory"},
  288. {0x6C, NULL, "SetHardwareBreakPoint"},
  289. {0x6D, NULL, "GetDebugThreadParam"},
  290. {0x6E, NULL, "Unknown"},
  291. {0x6F, NULL, "Unknown"},
  292. {0x70, NULL, "ControlProcessMemory"},
  293. {0x71, NULL, "MapProcessMemory"},
  294. {0x72, NULL, "UnmapProcessMemory"},
  295. {0x73, NULL, "Unknown"},
  296. {0x74, NULL, "Unknown"},
  297. {0x75, NULL, "Unknown"},
  298. {0x76, NULL, "TerminateProcess"},
  299. {0x77, NULL, "Unknown"},
  300. {0x78, NULL, "CreateResourceLimit"},
  301. {0x79, NULL, "Unknown"},
  302. {0x7A, NULL, "Unknown"},
  303. {0x7B, NULL, "Unknown"},
  304. {0x7C, NULL, "KernelSetState"},
  305. {0x7D, NULL, "QueryProcessMemory"},
  306. };
  307. void Register() {
  308. HLE::RegisterModule("SVC_Table", ARRAY_SIZE(SVC_Table), SVC_Table);
  309. }
  310. } // namespace