svc.cpp 18 KB

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