syscall.cpp 15 KB

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