svc.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cinttypes>
  6. #include <iterator>
  7. #include "common/logging/log.h"
  8. #include "common/microprofile.h"
  9. #include "common/string_util.h"
  10. #include "core/core.h"
  11. #include "core/core_timing.h"
  12. #include "core/hle/kernel/client_port.h"
  13. #include "core/hle/kernel/client_session.h"
  14. #include "core/hle/kernel/event.h"
  15. #include "core/hle/kernel/handle_table.h"
  16. #include "core/hle/kernel/mutex.h"
  17. #include "core/hle/kernel/object_address_table.h"
  18. #include "core/hle/kernel/process.h"
  19. #include "core/hle/kernel/resource_limit.h"
  20. #include "core/hle/kernel/shared_memory.h"
  21. #include "core/hle/kernel/svc.h"
  22. #include "core/hle/kernel/svc_wrap.h"
  23. #include "core/hle/kernel/thread.h"
  24. #include "core/hle/lock.h"
  25. #include "core/hle/result.h"
  26. #include "core/hle/service/service.h"
  27. namespace Kernel {
  28. /// Set the process heap to a given Size. It can both extend and shrink the heap.
  29. static ResultCode SetHeapSize(VAddr* heap_addr, u64 heap_size) {
  30. NGLOG_TRACE(Kernel_SVC, "called, heap_size=0x{:X}", heap_size);
  31. auto& process = *Core::CurrentProcess();
  32. CASCADE_RESULT(*heap_addr,
  33. process.HeapAllocate(Memory::HEAP_VADDR, heap_size, VMAPermission::ReadWrite));
  34. return RESULT_SUCCESS;
  35. }
  36. static ResultCode SetMemoryAttribute(VAddr addr, u64 size, u32 state0, u32 state1) {
  37. NGLOG_WARNING(Kernel_SVC, "(STUBBED) called, addr=0x{:X}", addr);
  38. return RESULT_SUCCESS;
  39. }
  40. /// Maps a memory range into a different range.
  41. static ResultCode MapMemory(VAddr dst_addr, VAddr src_addr, u64 size) {
  42. NGLOG_TRACE(Kernel_SVC, "called, dst_addr=0x{:X}, src_addr=0x{:X}, size=0x{:X}", dst_addr,
  43. src_addr, size);
  44. return Core::CurrentProcess()->MirrorMemory(dst_addr, src_addr, size);
  45. }
  46. /// Unmaps a region that was previously mapped with svcMapMemory
  47. static ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size) {
  48. NGLOG_TRACE(Kernel_SVC, "called, dst_addr=0x{:X}, src_addr=0x{:X}, size=0x{:X}", dst_addr,
  49. src_addr, size);
  50. return Core::CurrentProcess()->UnmapMemory(dst_addr, src_addr, size);
  51. }
  52. /// Connect to an OS service given the port name, returns the handle to the port to out
  53. static ResultCode ConnectToNamedPort(Handle* out_handle, VAddr port_name_address) {
  54. if (!Memory::IsValidVirtualAddress(port_name_address))
  55. return ERR_NOT_FOUND;
  56. static constexpr std::size_t PortNameMaxLength = 11;
  57. // Read 1 char beyond the max allowed port name to detect names that are too long.
  58. std::string port_name = Memory::ReadCString(port_name_address, PortNameMaxLength + 1);
  59. if (port_name.size() > PortNameMaxLength)
  60. return ERR_PORT_NAME_TOO_LONG;
  61. NGLOG_TRACE(Kernel_SVC, "called port_name={}", port_name);
  62. auto it = Service::g_kernel_named_ports.find(port_name);
  63. if (it == Service::g_kernel_named_ports.end()) {
  64. NGLOG_WARNING(Kernel_SVC, "tried to connect to unknown port: {}", port_name);
  65. return ERR_NOT_FOUND;
  66. }
  67. auto client_port = it->second;
  68. SharedPtr<ClientSession> client_session;
  69. CASCADE_RESULT(client_session, client_port->Connect());
  70. // Return the client session
  71. CASCADE_RESULT(*out_handle, g_handle_table.Create(client_session));
  72. return RESULT_SUCCESS;
  73. }
  74. /// Makes a blocking IPC call to an OS service.
  75. static ResultCode SendSyncRequest(Handle handle) {
  76. SharedPtr<ClientSession> session = g_handle_table.Get<ClientSession>(handle);
  77. if (!session) {
  78. NGLOG_ERROR(Kernel_SVC, "called with invalid handle=0x{:08X}", handle);
  79. return ERR_INVALID_HANDLE;
  80. }
  81. NGLOG_TRACE(Kernel_SVC, "called handle=0x{:08X}({})", handle, session->GetName());
  82. Core::System::GetInstance().PrepareReschedule();
  83. // TODO(Subv): svcSendSyncRequest should put the caller thread to sleep while the server
  84. // responds and cause a reschedule.
  85. return session->SendSyncRequest(GetCurrentThread());
  86. }
  87. /// Get the ID for the specified thread.
  88. static ResultCode GetThreadId(u32* thread_id, Handle thread_handle) {
  89. NGLOG_TRACE(Kernel_SVC, "called thread=0x{:08X}", thread_handle);
  90. const SharedPtr<Thread> thread = g_handle_table.Get<Thread>(thread_handle);
  91. if (!thread) {
  92. return ERR_INVALID_HANDLE;
  93. }
  94. *thread_id = thread->GetThreadId();
  95. return RESULT_SUCCESS;
  96. }
  97. /// Get the ID of the specified process
  98. static ResultCode GetProcessId(u32* process_id, Handle process_handle) {
  99. NGLOG_TRACE(Kernel_SVC, "called process=0x{:08X}", process_handle);
  100. const SharedPtr<Process> process = g_handle_table.Get<Process>(process_handle);
  101. if (!process) {
  102. return ERR_INVALID_HANDLE;
  103. }
  104. *process_id = process->process_id;
  105. return RESULT_SUCCESS;
  106. }
  107. /// Default thread wakeup callback for WaitSynchronization
  108. static bool DefaultThreadWakeupCallback(ThreadWakeupReason reason, SharedPtr<Thread> thread,
  109. SharedPtr<WaitObject> object, size_t index) {
  110. ASSERT(thread->status == THREADSTATUS_WAIT_SYNCH_ANY);
  111. if (reason == ThreadWakeupReason::Timeout) {
  112. thread->SetWaitSynchronizationResult(RESULT_TIMEOUT);
  113. return true;
  114. }
  115. ASSERT(reason == ThreadWakeupReason::Signal);
  116. thread->SetWaitSynchronizationResult(RESULT_SUCCESS);
  117. thread->SetWaitSynchronizationOutput(static_cast<u32>(index));
  118. return true;
  119. };
  120. /// Wait for the given handles to synchronize, timeout after the specified nanoseconds
  121. static ResultCode WaitSynchronization(Handle* index, VAddr handles_address, u64 handle_count,
  122. s64 nano_seconds) {
  123. NGLOG_TRACE(Kernel_SVC, "called handles_address=0x{:X}, handle_count={}, nano_seconds={}",
  124. handles_address, handle_count, nano_seconds);
  125. if (!Memory::IsValidVirtualAddress(handles_address))
  126. return ERR_INVALID_POINTER;
  127. static constexpr u64 MaxHandles = 0x40;
  128. if (handle_count > MaxHandles)
  129. return ResultCode(ErrorModule::Kernel, ErrCodes::TooLarge);
  130. auto thread = GetCurrentThread();
  131. using ObjectPtr = SharedPtr<WaitObject>;
  132. std::vector<ObjectPtr> objects(handle_count);
  133. for (int i = 0; i < handle_count; ++i) {
  134. Handle handle = Memory::Read32(handles_address + i * sizeof(Handle));
  135. auto object = g_handle_table.Get<WaitObject>(handle);
  136. if (object == nullptr)
  137. return ERR_INVALID_HANDLE;
  138. objects[i] = object;
  139. }
  140. // Find the first object that is acquirable in the provided list of objects
  141. auto itr = std::find_if(objects.begin(), objects.end(), [thread](const ObjectPtr& object) {
  142. return !object->ShouldWait(thread);
  143. });
  144. if (itr != objects.end()) {
  145. // We found a ready object, acquire it and set the result value
  146. WaitObject* object = itr->get();
  147. object->Acquire(thread);
  148. *index = static_cast<s32>(std::distance(objects.begin(), itr));
  149. return RESULT_SUCCESS;
  150. }
  151. // No objects were ready to be acquired, prepare to suspend the thread.
  152. // If a timeout value of 0 was provided, just return the Timeout error code instead of
  153. // suspending the thread.
  154. if (nano_seconds == 0)
  155. return RESULT_TIMEOUT;
  156. for (auto& object : objects)
  157. object->AddWaitingThread(thread);
  158. thread->wait_objects = std::move(objects);
  159. thread->status = THREADSTATUS_WAIT_SYNCH_ANY;
  160. // Create an event to wake the thread up after the specified nanosecond delay has passed
  161. thread->WakeAfterDelay(nano_seconds);
  162. thread->wakeup_callback = DefaultThreadWakeupCallback;
  163. Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule();
  164. return RESULT_TIMEOUT;
  165. }
  166. /// Resumes a thread waiting on WaitSynchronization
  167. static ResultCode CancelSynchronization(Handle thread_handle) {
  168. NGLOG_TRACE(Kernel_SVC, "called thread=0x{:X}", thread_handle);
  169. const SharedPtr<Thread> thread = g_handle_table.Get<Thread>(thread_handle);
  170. if (!thread) {
  171. return ERR_INVALID_HANDLE;
  172. }
  173. ASSERT(thread->status == THREADSTATUS_WAIT_SYNCH_ANY);
  174. thread->SetWaitSynchronizationResult(
  175. ResultCode(ErrorModule::Kernel, ErrCodes::SynchronizationCanceled));
  176. thread->ResumeFromWait();
  177. return RESULT_SUCCESS;
  178. }
  179. /// Attempts to locks a mutex, creating it if it does not already exist
  180. static ResultCode ArbitrateLock(Handle holding_thread_handle, VAddr mutex_addr,
  181. Handle requesting_thread_handle) {
  182. NGLOG_TRACE(Kernel_SVC,
  183. "called holding_thread_handle=0x{:08X}, mutex_addr=0x{:X}, "
  184. "requesting_current_thread_handle=0x{:08X}",
  185. holding_thread_handle, mutex_addr, requesting_thread_handle);
  186. return Mutex::TryAcquire(mutex_addr, holding_thread_handle, requesting_thread_handle);
  187. }
  188. /// Unlock a mutex
  189. static ResultCode ArbitrateUnlock(VAddr mutex_addr) {
  190. NGLOG_TRACE(Kernel_SVC, "called mutex_addr=0x{:X}", mutex_addr);
  191. return Mutex::Release(mutex_addr);
  192. }
  193. /// Break program execution
  194. static void Break(u64 unk_0, u64 unk_1, u64 unk_2) {
  195. NGLOG_CRITICAL(Debug_Emulated, "Emulated program broke execution!");
  196. ASSERT(false);
  197. }
  198. /// Used to output a message on a debug hardware unit - does nothing on a retail unit
  199. static void OutputDebugString(VAddr address, s32 len) {
  200. std::string str(len, '\0');
  201. Memory::ReadBlock(address, str.data(), str.size());
  202. NGLOG_DEBUG(Debug_Emulated, "{}", str);
  203. }
  204. /// Gets system/memory information for the current process
  205. static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) {
  206. NGLOG_TRACE(Kernel_SVC, "called info_id=0x{:X}, info_sub_id=0x{:X}, handle=0x{:08X}", info_id,
  207. info_sub_id, handle);
  208. auto& vm_manager = Core::CurrentProcess()->vm_manager;
  209. switch (static_cast<GetInfoType>(info_id)) {
  210. case GetInfoType::AllowedCpuIdBitmask:
  211. *result = Core::CurrentProcess()->allowed_processor_mask;
  212. break;
  213. case GetInfoType::AllowedThreadPrioBitmask:
  214. *result = Core::CurrentProcess()->allowed_thread_priority_mask;
  215. break;
  216. case GetInfoType::MapRegionBaseAddr:
  217. *result = Memory::MAP_REGION_VADDR;
  218. break;
  219. case GetInfoType::MapRegionSize:
  220. *result = Memory::MAP_REGION_SIZE;
  221. break;
  222. case GetInfoType::HeapRegionBaseAddr:
  223. *result = Memory::HEAP_VADDR;
  224. break;
  225. case GetInfoType::HeapRegionSize:
  226. *result = Memory::HEAP_SIZE;
  227. break;
  228. case GetInfoType::TotalMemoryUsage:
  229. *result = vm_manager.GetTotalMemoryUsage();
  230. break;
  231. case GetInfoType::TotalHeapUsage:
  232. *result = vm_manager.GetTotalHeapUsage();
  233. break;
  234. case GetInfoType::IsCurrentProcessBeingDebugged:
  235. *result = 0;
  236. break;
  237. case GetInfoType::RandomEntropy:
  238. *result = 0;
  239. break;
  240. case GetInfoType::AddressSpaceBaseAddr:
  241. *result = vm_manager.GetAddressSpaceBaseAddr();
  242. break;
  243. case GetInfoType::AddressSpaceSize:
  244. *result = vm_manager.GetAddressSpaceSize();
  245. break;
  246. case GetInfoType::NewMapRegionBaseAddr:
  247. *result = Memory::NEW_MAP_REGION_VADDR;
  248. break;
  249. case GetInfoType::NewMapRegionSize:
  250. *result = Memory::NEW_MAP_REGION_SIZE;
  251. break;
  252. case GetInfoType::IsVirtualAddressMemoryEnabled:
  253. *result = Core::CurrentProcess()->is_virtual_address_memory_enabled;
  254. break;
  255. case GetInfoType::TitleId:
  256. NGLOG_WARNING(Kernel_SVC, "(STUBBED) Attempted to query titleid, returned 0");
  257. *result = 0;
  258. break;
  259. case GetInfoType::PrivilegedProcessId:
  260. NGLOG_WARNING(Kernel_SVC,
  261. "(STUBBED) Attempted to query privileged process id bounds, returned 0");
  262. *result = 0;
  263. break;
  264. default:
  265. UNIMPLEMENTED();
  266. }
  267. return RESULT_SUCCESS;
  268. }
  269. /// Sets the thread activity
  270. static ResultCode SetThreadActivity(Handle handle, u32 unknown) {
  271. NGLOG_WARNING(Kernel_SVC, "(STUBBED) called, handle=0x{:08X}, unknown=0x{:08X}", handle,
  272. unknown);
  273. return RESULT_SUCCESS;
  274. }
  275. /// Gets the thread context
  276. static ResultCode GetThreadContext(Handle handle, VAddr addr) {
  277. NGLOG_WARNING(Kernel_SVC, "(STUBBED) called, handle=0x{:08X}, addr=0x{:X}", handle, addr);
  278. return RESULT_SUCCESS;
  279. }
  280. /// Gets the priority for the specified thread
  281. static ResultCode GetThreadPriority(u32* priority, Handle handle) {
  282. const SharedPtr<Thread> thread = g_handle_table.Get<Thread>(handle);
  283. if (!thread)
  284. return ERR_INVALID_HANDLE;
  285. *priority = thread->GetPriority();
  286. return RESULT_SUCCESS;
  287. }
  288. /// Sets the priority for the specified thread
  289. static ResultCode SetThreadPriority(Handle handle, u32 priority) {
  290. if (priority > THREADPRIO_LOWEST) {
  291. return ERR_OUT_OF_RANGE;
  292. }
  293. SharedPtr<Thread> thread = g_handle_table.Get<Thread>(handle);
  294. if (!thread)
  295. return ERR_INVALID_HANDLE;
  296. // Note: The kernel uses the current process's resource limit instead of
  297. // the one from the thread owner's resource limit.
  298. SharedPtr<ResourceLimit>& resource_limit = Core::CurrentProcess()->resource_limit;
  299. if (resource_limit->GetMaxResourceValue(ResourceType::Priority) > priority) {
  300. return ERR_NOT_AUTHORIZED;
  301. }
  302. thread->SetPriority(priority);
  303. Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule();
  304. return RESULT_SUCCESS;
  305. }
  306. /// Get which CPU core is executing the current thread
  307. static u32 GetCurrentProcessorNumber() {
  308. NGLOG_TRACE(Kernel_SVC, "called");
  309. return GetCurrentThread()->processor_id;
  310. }
  311. static ResultCode MapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 size,
  312. u32 permissions) {
  313. NGLOG_TRACE(
  314. Kernel_SVC,
  315. "called, shared_memory_handle=0x{:X}, addr=0x{:X}, size=0x{:X}, permissions=0x{:08X}",
  316. shared_memory_handle, addr, size, permissions);
  317. SharedPtr<SharedMemory> shared_memory = g_handle_table.Get<SharedMemory>(shared_memory_handle);
  318. if (!shared_memory) {
  319. return ERR_INVALID_HANDLE;
  320. }
  321. MemoryPermission permissions_type = static_cast<MemoryPermission>(permissions);
  322. switch (permissions_type) {
  323. case MemoryPermission::Read:
  324. case MemoryPermission::Write:
  325. case MemoryPermission::ReadWrite:
  326. case MemoryPermission::Execute:
  327. case MemoryPermission::ReadExecute:
  328. case MemoryPermission::WriteExecute:
  329. case MemoryPermission::ReadWriteExecute:
  330. case MemoryPermission::DontCare:
  331. return shared_memory->Map(Core::CurrentProcess().get(), addr, permissions_type,
  332. MemoryPermission::DontCare);
  333. default:
  334. NGLOG_ERROR(Kernel_SVC, "unknown permissions=0x{:08X}", permissions);
  335. }
  336. return RESULT_SUCCESS;
  337. }
  338. static ResultCode UnmapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 size) {
  339. NGLOG_WARNING(Kernel_SVC, "called, shared_memory_handle=0x{:08X}, addr=0x{:X}, size=0x{:X}",
  340. shared_memory_handle, addr, size);
  341. SharedPtr<SharedMemory> shared_memory = g_handle_table.Get<SharedMemory>(shared_memory_handle);
  342. return shared_memory->Unmap(Core::CurrentProcess().get(), addr);
  343. }
  344. /// Query process memory
  345. static ResultCode QueryProcessMemory(MemoryInfo* memory_info, PageInfo* /*page_info*/,
  346. Handle process_handle, u64 addr) {
  347. SharedPtr<Process> process = g_handle_table.Get<Process>(process_handle);
  348. if (!process) {
  349. return ERR_INVALID_HANDLE;
  350. }
  351. auto vma = process->vm_manager.FindVMA(addr);
  352. memory_info->attributes = 0;
  353. if (vma == Core::CurrentProcess()->vm_manager.vma_map.end()) {
  354. memory_info->base_address = 0;
  355. memory_info->permission = static_cast<u32>(VMAPermission::None);
  356. memory_info->size = 0;
  357. memory_info->type = static_cast<u32>(MemoryState::Unmapped);
  358. } else {
  359. memory_info->base_address = vma->second.base;
  360. memory_info->permission = static_cast<u32>(vma->second.permissions);
  361. memory_info->size = vma->second.size;
  362. memory_info->type = static_cast<u32>(vma->second.meminfo_state);
  363. }
  364. NGLOG_TRACE(Kernel_SVC, "called process=0x{:08X} addr={:X}", process_handle, addr);
  365. return RESULT_SUCCESS;
  366. }
  367. /// Query memory
  368. static ResultCode QueryMemory(MemoryInfo* memory_info, PageInfo* page_info, VAddr addr) {
  369. NGLOG_TRACE(Kernel_SVC, "called, addr={:X}", addr);
  370. return QueryProcessMemory(memory_info, page_info, CurrentProcess, addr);
  371. }
  372. /// Exits the current process
  373. static void ExitProcess() {
  374. NGLOG_INFO(Kernel_SVC, "Process {} exiting", Core::CurrentProcess()->process_id);
  375. ASSERT_MSG(Core::CurrentProcess()->status == ProcessStatus::Running,
  376. "Process has already exited");
  377. Core::CurrentProcess()->status = ProcessStatus::Exited;
  378. auto stop_threads = [](const std::vector<SharedPtr<Thread>>& thread_list) {
  379. for (auto& thread : thread_list) {
  380. if (thread->owner_process != Core::CurrentProcess())
  381. continue;
  382. if (thread == GetCurrentThread())
  383. continue;
  384. // TODO(Subv): When are the other running/ready threads terminated?
  385. ASSERT_MSG(thread->status == THREADSTATUS_WAIT_SYNCH_ANY ||
  386. thread->status == THREADSTATUS_WAIT_SYNCH_ALL,
  387. "Exiting processes with non-waiting threads is currently unimplemented");
  388. thread->Stop();
  389. }
  390. };
  391. auto& system = Core::System::GetInstance();
  392. stop_threads(system.Scheduler(0)->GetThreadList());
  393. stop_threads(system.Scheduler(1)->GetThreadList());
  394. stop_threads(system.Scheduler(2)->GetThreadList());
  395. stop_threads(system.Scheduler(3)->GetThreadList());
  396. // Kill the current thread
  397. GetCurrentThread()->Stop();
  398. Core::System::GetInstance().PrepareReschedule();
  399. }
  400. /// Creates a new thread
  401. static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, VAddr stack_top,
  402. u32 priority, s32 processor_id) {
  403. std::string name = fmt::format("unknown-{:X}", entry_point);
  404. if (priority > THREADPRIO_LOWEST) {
  405. return ERR_OUT_OF_RANGE;
  406. }
  407. SharedPtr<ResourceLimit>& resource_limit = Core::CurrentProcess()->resource_limit;
  408. if (resource_limit->GetMaxResourceValue(ResourceType::Priority) > priority) {
  409. return ERR_NOT_AUTHORIZED;
  410. }
  411. if (processor_id == THREADPROCESSORID_DEFAULT) {
  412. // Set the target CPU to the one specified in the process' exheader.
  413. processor_id = Core::CurrentProcess()->ideal_processor;
  414. ASSERT(processor_id != THREADPROCESSORID_DEFAULT);
  415. }
  416. switch (processor_id) {
  417. case THREADPROCESSORID_0:
  418. case THREADPROCESSORID_1:
  419. case THREADPROCESSORID_2:
  420. case THREADPROCESSORID_3:
  421. break;
  422. default:
  423. ASSERT_MSG(false, "Unsupported thread processor ID: {}", processor_id);
  424. break;
  425. }
  426. CASCADE_RESULT(SharedPtr<Thread> thread,
  427. Thread::Create(name, entry_point, priority, arg, processor_id, stack_top,
  428. Core::CurrentProcess()));
  429. CASCADE_RESULT(thread->guest_handle, g_handle_table.Create(thread));
  430. *out_handle = thread->guest_handle;
  431. Core::System::GetInstance().PrepareReschedule();
  432. Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule();
  433. NGLOG_TRACE(Kernel_SVC,
  434. "called entrypoint=0x{:08X} ({}), arg=0x{:08X}, stacktop=0x{:08X}, "
  435. "threadpriority=0x{:08X}, processorid=0x{:08X} : created handle=0x{:08X}",
  436. entry_point, name, arg, stack_top, priority, processor_id, *out_handle);
  437. return RESULT_SUCCESS;
  438. }
  439. /// Starts the thread for the provided handle
  440. static ResultCode StartThread(Handle thread_handle) {
  441. NGLOG_TRACE(Kernel_SVC, "called thread=0x{:08X}", thread_handle);
  442. const SharedPtr<Thread> thread = g_handle_table.Get<Thread>(thread_handle);
  443. if (!thread) {
  444. return ERR_INVALID_HANDLE;
  445. }
  446. ASSERT(thread->status == THREADSTATUS_DORMANT);
  447. thread->ResumeFromWait();
  448. Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule();
  449. return RESULT_SUCCESS;
  450. }
  451. /// Called when a thread exits
  452. static void ExitThread() {
  453. NGLOG_TRACE(Kernel_SVC, "called, pc=0x{:08X}", Core::CurrentArmInterface().GetPC());
  454. ExitCurrentThread();
  455. Core::System::GetInstance().PrepareReschedule();
  456. }
  457. /// Sleep the current thread
  458. static void SleepThread(s64 nanoseconds) {
  459. NGLOG_TRACE(Kernel_SVC, "called nanoseconds={}", nanoseconds);
  460. // Don't attempt to yield execution if there are no available threads to run,
  461. // this way we avoid a useless reschedule to the idle thread.
  462. if (nanoseconds == 0 && !Core::System::GetInstance().CurrentScheduler().HaveReadyThreads())
  463. return;
  464. // Sleep current thread and check for next thread to schedule
  465. WaitCurrentThread_Sleep();
  466. // Create an event to wake the thread up after the specified nanosecond delay has passed
  467. GetCurrentThread()->WakeAfterDelay(nanoseconds);
  468. Core::System::GetInstance().PrepareReschedule();
  469. }
  470. /// Signal process wide key atomic
  471. static ResultCode WaitProcessWideKeyAtomic(VAddr mutex_addr, VAddr condition_variable_addr,
  472. Handle thread_handle, s64 nano_seconds) {
  473. NGLOG_TRACE(
  474. Kernel_SVC,
  475. "called mutex_addr={:X}, condition_variable_addr={:X}, thread_handle=0x{:08X}, timeout={}",
  476. mutex_addr, condition_variable_addr, thread_handle, nano_seconds);
  477. SharedPtr<Thread> thread = g_handle_table.Get<Thread>(thread_handle);
  478. ASSERT(thread);
  479. CASCADE_CODE(Mutex::Release(mutex_addr));
  480. SharedPtr<Thread> current_thread = GetCurrentThread();
  481. current_thread->condvar_wait_address = condition_variable_addr;
  482. current_thread->mutex_wait_address = mutex_addr;
  483. current_thread->wait_handle = thread_handle;
  484. current_thread->status = THREADSTATUS_WAIT_MUTEX;
  485. current_thread->wakeup_callback = nullptr;
  486. current_thread->WakeAfterDelay(nano_seconds);
  487. // Note: Deliberately don't attempt to inherit the lock owner's priority.
  488. Core::System::GetInstance().CpuCore(current_thread->processor_id).PrepareReschedule();
  489. return RESULT_SUCCESS;
  490. }
  491. /// Signal process wide key
  492. static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target) {
  493. NGLOG_TRACE(Kernel_SVC, "called, condition_variable_addr=0x{:X}, target=0x{:08X}",
  494. condition_variable_addr, target);
  495. auto RetrieveWaitingThreads =
  496. [](size_t core_index, std::vector<SharedPtr<Thread>>& waiting_threads, VAddr condvar_addr) {
  497. const auto& scheduler = Core::System::GetInstance().Scheduler(core_index);
  498. auto& thread_list = scheduler->GetThreadList();
  499. for (auto& thread : thread_list) {
  500. if (thread->condvar_wait_address == condvar_addr)
  501. waiting_threads.push_back(thread);
  502. }
  503. };
  504. // Retrieve a list of all threads that are waiting for this condition variable.
  505. std::vector<SharedPtr<Thread>> waiting_threads;
  506. RetrieveWaitingThreads(0, waiting_threads, condition_variable_addr);
  507. RetrieveWaitingThreads(1, waiting_threads, condition_variable_addr);
  508. RetrieveWaitingThreads(2, waiting_threads, condition_variable_addr);
  509. RetrieveWaitingThreads(3, waiting_threads, condition_variable_addr);
  510. // Sort them by priority, such that the highest priority ones come first.
  511. std::sort(waiting_threads.begin(), waiting_threads.end(),
  512. [](const SharedPtr<Thread>& lhs, const SharedPtr<Thread>& rhs) {
  513. return lhs->current_priority < rhs->current_priority;
  514. });
  515. // Only process up to 'target' threads, unless 'target' is -1, in which case process
  516. // them all.
  517. size_t last = waiting_threads.size();
  518. if (target != -1)
  519. last = target;
  520. // If there are no threads waiting on this condition variable, just exit
  521. if (last > waiting_threads.size())
  522. return RESULT_SUCCESS;
  523. for (size_t index = 0; index < last; ++index) {
  524. auto& thread = waiting_threads[index];
  525. ASSERT(thread->condvar_wait_address == condition_variable_addr);
  526. // If the mutex is not yet acquired, acquire it.
  527. u32 mutex_val = Memory::Read32(thread->mutex_wait_address);
  528. if (mutex_val == 0) {
  529. // We were able to acquire the mutex, resume this thread.
  530. Memory::Write32(thread->mutex_wait_address, thread->wait_handle);
  531. ASSERT(thread->status == THREADSTATUS_WAIT_MUTEX);
  532. thread->ResumeFromWait();
  533. auto lock_owner = thread->lock_owner;
  534. if (lock_owner)
  535. lock_owner->RemoveMutexWaiter(thread);
  536. thread->lock_owner = nullptr;
  537. thread->mutex_wait_address = 0;
  538. thread->condvar_wait_address = 0;
  539. thread->wait_handle = 0;
  540. } else {
  541. // Couldn't acquire the mutex, block the thread.
  542. Handle owner_handle = static_cast<Handle>(mutex_val & Mutex::MutexOwnerMask);
  543. auto owner = g_handle_table.Get<Thread>(owner_handle);
  544. ASSERT(owner);
  545. ASSERT(thread->status != THREADSTATUS_RUNNING);
  546. thread->status = THREADSTATUS_WAIT_MUTEX;
  547. thread->wakeup_callback = nullptr;
  548. // Signal that the mutex now has a waiting thread.
  549. Memory::Write32(thread->mutex_wait_address, mutex_val | Mutex::MutexHasWaitersFlag);
  550. owner->AddMutexWaiter(thread);
  551. Core::System::GetInstance().CpuCore(thread->processor_id).PrepareReschedule();
  552. }
  553. }
  554. return RESULT_SUCCESS;
  555. }
  556. /// This returns the total CPU ticks elapsed since the CPU was powered-on
  557. static u64 GetSystemTick() {
  558. const u64 result{CoreTiming::GetTicks()};
  559. // Advance time to defeat dumb games that busy-wait for the frame to end.
  560. CoreTiming::AddTicks(400);
  561. return result;
  562. }
  563. /// Close a handle
  564. static ResultCode CloseHandle(Handle handle) {
  565. NGLOG_TRACE(Kernel_SVC, "Closing handle 0x{:08X}", handle);
  566. return g_handle_table.Close(handle);
  567. }
  568. /// Reset an event
  569. static ResultCode ResetSignal(Handle handle) {
  570. NGLOG_WARNING(Kernel_SVC, "(STUBBED) called handle 0x{:08X}", handle);
  571. auto event = g_handle_table.Get<Event>(handle);
  572. ASSERT(event != nullptr);
  573. event->Clear();
  574. return RESULT_SUCCESS;
  575. }
  576. /// Creates a TransferMemory object
  577. static ResultCode CreateTransferMemory(Handle* handle, VAddr addr, u64 size, u32 permissions) {
  578. NGLOG_WARNING(Kernel_SVC, "(STUBBED) called addr=0x{:X}, size=0x{:X}, perms=0x{:08X}", addr,
  579. size, permissions);
  580. *handle = 0;
  581. return RESULT_SUCCESS;
  582. }
  583. static ResultCode GetThreadCoreMask(Handle thread_handle, u32* core, u64* mask) {
  584. NGLOG_TRACE(Kernel_SVC, "called, handle=0x{:08X}", thread_handle);
  585. const SharedPtr<Thread> thread = g_handle_table.Get<Thread>(thread_handle);
  586. if (!thread) {
  587. return ERR_INVALID_HANDLE;
  588. }
  589. *core = thread->ideal_core;
  590. *mask = thread->affinity_mask;
  591. return RESULT_SUCCESS;
  592. }
  593. static ResultCode SetThreadCoreMask(Handle thread_handle, u32 core, u64 mask) {
  594. NGLOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, mask=0x{:16X}, core=0x{:X}", thread_handle,
  595. mask, core);
  596. const SharedPtr<Thread> thread = g_handle_table.Get<Thread>(thread_handle);
  597. if (!thread) {
  598. return ERR_INVALID_HANDLE;
  599. }
  600. if (core == THREADPROCESSORID_DEFAULT) {
  601. ASSERT(thread->owner_process->ideal_processor != THREADPROCESSORID_DEFAULT);
  602. // Set the target CPU to the one specified in the process' exheader.
  603. core = thread->owner_process->ideal_processor;
  604. mask = 1 << core;
  605. }
  606. if (mask == 0) {
  607. return ResultCode(ErrorModule::Kernel, ErrCodes::InvalidCombination);
  608. }
  609. /// This value is used to only change the affinity mask without changing the current ideal core.
  610. static constexpr u32 OnlyChangeMask = static_cast<u32>(-3);
  611. if (core == OnlyChangeMask) {
  612. core = thread->ideal_core;
  613. } else if (core >= Core::NUM_CPU_CORES && core != -1) {
  614. return ResultCode(ErrorModule::Kernel, ErrCodes::InvalidProcessorId);
  615. }
  616. // Error out if the input core isn't enabled in the input mask.
  617. if (core < Core::NUM_CPU_CORES && (mask & (1 << core)) == 0) {
  618. return ResultCode(ErrorModule::Kernel, ErrCodes::InvalidCombination);
  619. }
  620. thread->ChangeCore(core, mask);
  621. return RESULT_SUCCESS;
  622. }
  623. static ResultCode CreateSharedMemory(Handle* handle, u64 size, u32 local_permissions,
  624. u32 remote_permissions) {
  625. NGLOG_TRACE(Kernel_SVC, "called, size=0x{:X}, localPerms=0x{:08X}, remotePerms=0x{:08X}", size,
  626. local_permissions, remote_permissions);
  627. auto sharedMemHandle =
  628. SharedMemory::Create(g_handle_table.Get<Process>(KernelHandle::CurrentProcess), size,
  629. static_cast<MemoryPermission>(local_permissions),
  630. static_cast<MemoryPermission>(remote_permissions));
  631. CASCADE_RESULT(*handle, g_handle_table.Create(sharedMemHandle));
  632. return RESULT_SUCCESS;
  633. }
  634. static ResultCode ClearEvent(Handle handle) {
  635. NGLOG_TRACE(Kernel_SVC, "called, event=0x{:08X}", handle);
  636. SharedPtr<Event> evt = g_handle_table.Get<Event>(handle);
  637. if (evt == nullptr)
  638. return ERR_INVALID_HANDLE;
  639. evt->Clear();
  640. return RESULT_SUCCESS;
  641. }
  642. namespace {
  643. struct FunctionDef {
  644. using Func = void();
  645. u32 id;
  646. Func* func;
  647. const char* name;
  648. };
  649. } // namespace
  650. static const FunctionDef SVC_Table[] = {
  651. {0x00, nullptr, "Unknown"},
  652. {0x01, SvcWrap<SetHeapSize>, "SetHeapSize"},
  653. {0x02, nullptr, "SetMemoryPermission"},
  654. {0x03, SvcWrap<SetMemoryAttribute>, "SetMemoryAttribute"},
  655. {0x04, SvcWrap<MapMemory>, "MapMemory"},
  656. {0x05, SvcWrap<UnmapMemory>, "UnmapMemory"},
  657. {0x06, SvcWrap<QueryMemory>, "QueryMemory"},
  658. {0x07, SvcWrap<ExitProcess>, "ExitProcess"},
  659. {0x08, SvcWrap<CreateThread>, "CreateThread"},
  660. {0x09, SvcWrap<StartThread>, "StartThread"},
  661. {0x0A, SvcWrap<ExitThread>, "ExitThread"},
  662. {0x0B, SvcWrap<SleepThread>, "SleepThread"},
  663. {0x0C, SvcWrap<GetThreadPriority>, "GetThreadPriority"},
  664. {0x0D, SvcWrap<SetThreadPriority>, "SetThreadPriority"},
  665. {0x0E, SvcWrap<GetThreadCoreMask>, "GetThreadCoreMask"},
  666. {0x0F, SvcWrap<SetThreadCoreMask>, "SetThreadCoreMask"},
  667. {0x10, SvcWrap<GetCurrentProcessorNumber>, "GetCurrentProcessorNumber"},
  668. {0x11, nullptr, "SignalEvent"},
  669. {0x12, SvcWrap<ClearEvent>, "ClearEvent"},
  670. {0x13, SvcWrap<MapSharedMemory>, "MapSharedMemory"},
  671. {0x14, SvcWrap<UnmapSharedMemory>, "UnmapSharedMemory"},
  672. {0x15, SvcWrap<CreateTransferMemory>, "CreateTransferMemory"},
  673. {0x16, SvcWrap<CloseHandle>, "CloseHandle"},
  674. {0x17, SvcWrap<ResetSignal>, "ResetSignal"},
  675. {0x18, SvcWrap<WaitSynchronization>, "WaitSynchronization"},
  676. {0x19, SvcWrap<CancelSynchronization>, "CancelSynchronization"},
  677. {0x1A, SvcWrap<ArbitrateLock>, "ArbitrateLock"},
  678. {0x1B, SvcWrap<ArbitrateUnlock>, "ArbitrateUnlock"},
  679. {0x1C, SvcWrap<WaitProcessWideKeyAtomic>, "WaitProcessWideKeyAtomic"},
  680. {0x1D, SvcWrap<SignalProcessWideKey>, "SignalProcessWideKey"},
  681. {0x1E, SvcWrap<GetSystemTick>, "GetSystemTick"},
  682. {0x1F, SvcWrap<ConnectToNamedPort>, "ConnectToNamedPort"},
  683. {0x20, nullptr, "SendSyncRequestLight"},
  684. {0x21, SvcWrap<SendSyncRequest>, "SendSyncRequest"},
  685. {0x22, nullptr, "SendSyncRequestWithUserBuffer"},
  686. {0x23, nullptr, "SendAsyncRequestWithUserBuffer"},
  687. {0x24, SvcWrap<GetProcessId>, "GetProcessId"},
  688. {0x25, SvcWrap<GetThreadId>, "GetThreadId"},
  689. {0x26, SvcWrap<Break>, "Break"},
  690. {0x27, SvcWrap<OutputDebugString>, "OutputDebugString"},
  691. {0x28, nullptr, "ReturnFromException"},
  692. {0x29, SvcWrap<GetInfo>, "GetInfo"},
  693. {0x2A, nullptr, "FlushEntireDataCache"},
  694. {0x2B, nullptr, "FlushDataCache"},
  695. {0x2C, nullptr, "MapPhysicalMemory"},
  696. {0x2D, nullptr, "UnmapPhysicalMemory"},
  697. {0x2E, nullptr, "GetNextThreadInfo"},
  698. {0x2F, nullptr, "GetLastThreadInfo"},
  699. {0x30, nullptr, "GetResourceLimitLimitValue"},
  700. {0x31, nullptr, "GetResourceLimitCurrentValue"},
  701. {0x32, SvcWrap<SetThreadActivity>, "SetThreadActivity"},
  702. {0x33, SvcWrap<GetThreadContext>, "GetThreadContext"},
  703. {0x34, nullptr, "WaitForAddress"},
  704. {0x35, nullptr, "SignalToAddress"},
  705. {0x36, nullptr, "Unknown"},
  706. {0x37, nullptr, "Unknown"},
  707. {0x38, nullptr, "Unknown"},
  708. {0x39, nullptr, "Unknown"},
  709. {0x3A, nullptr, "Unknown"},
  710. {0x3B, nullptr, "Unknown"},
  711. {0x3C, nullptr, "DumpInfo"},
  712. {0x3D, nullptr, "DumpInfoNew"},
  713. {0x3E, nullptr, "Unknown"},
  714. {0x3F, nullptr, "Unknown"},
  715. {0x40, nullptr, "CreateSession"},
  716. {0x41, nullptr, "AcceptSession"},
  717. {0x42, nullptr, "ReplyAndReceiveLight"},
  718. {0x43, nullptr, "ReplyAndReceive"},
  719. {0x44, nullptr, "ReplyAndReceiveWithUserBuffer"},
  720. {0x45, nullptr, "CreateEvent"},
  721. {0x46, nullptr, "Unknown"},
  722. {0x47, nullptr, "Unknown"},
  723. {0x48, nullptr, "AllocateUnsafeMemory"},
  724. {0x49, nullptr, "FreeUnsafeMemory"},
  725. {0x4A, nullptr, "SetUnsafeAllocationLimit"},
  726. {0x4B, nullptr, "CreateJitMemory"},
  727. {0x4C, nullptr, "MapJitMemory"},
  728. {0x4D, nullptr, "SleepSystem"},
  729. {0x4E, nullptr, "ReadWriteRegister"},
  730. {0x4F, nullptr, "SetProcessActivity"},
  731. {0x50, SvcWrap<CreateSharedMemory>, "CreateSharedMemory"},
  732. {0x51, nullptr, "MapTransferMemory"},
  733. {0x52, nullptr, "UnmapTransferMemory"},
  734. {0x53, nullptr, "CreateInterruptEvent"},
  735. {0x54, nullptr, "QueryPhysicalAddress"},
  736. {0x55, nullptr, "QueryIoMapping"},
  737. {0x56, nullptr, "CreateDeviceAddressSpace"},
  738. {0x57, nullptr, "AttachDeviceAddressSpace"},
  739. {0x58, nullptr, "DetachDeviceAddressSpace"},
  740. {0x59, nullptr, "MapDeviceAddressSpaceByForce"},
  741. {0x5A, nullptr, "MapDeviceAddressSpaceAligned"},
  742. {0x5B, nullptr, "MapDeviceAddressSpace"},
  743. {0x5C, nullptr, "UnmapDeviceAddressSpace"},
  744. {0x5D, nullptr, "InvalidateProcessDataCache"},
  745. {0x5E, nullptr, "StoreProcessDataCache"},
  746. {0x5F, nullptr, "FlushProcessDataCache"},
  747. {0x60, nullptr, "DebugActiveProcess"},
  748. {0x61, nullptr, "BreakDebugProcess"},
  749. {0x62, nullptr, "TerminateDebugProcess"},
  750. {0x63, nullptr, "GetDebugEvent"},
  751. {0x64, nullptr, "ContinueDebugEvent"},
  752. {0x65, nullptr, "GetProcessList"},
  753. {0x66, nullptr, "GetThreadList"},
  754. {0x67, nullptr, "GetDebugThreadContext"},
  755. {0x68, nullptr, "SetDebugThreadContext"},
  756. {0x69, nullptr, "QueryDebugProcessMemory"},
  757. {0x6A, nullptr, "ReadDebugProcessMemory"},
  758. {0x6B, nullptr, "WriteDebugProcessMemory"},
  759. {0x6C, nullptr, "SetHardwareBreakPoint"},
  760. {0x6D, nullptr, "GetDebugThreadParam"},
  761. {0x6E, nullptr, "Unknown"},
  762. {0x6F, nullptr, "GetMemoryInfo"},
  763. {0x70, nullptr, "CreatePort"},
  764. {0x71, nullptr, "ManageNamedPort"},
  765. {0x72, nullptr, "ConnectToPort"},
  766. {0x73, nullptr, "SetProcessMemoryPermission"},
  767. {0x74, nullptr, "MapProcessMemory"},
  768. {0x75, nullptr, "UnmapProcessMemory"},
  769. {0x76, nullptr, "QueryProcessMemory"},
  770. {0x77, nullptr, "MapProcessCodeMemory"},
  771. {0x78, nullptr, "UnmapProcessCodeMemory"},
  772. {0x79, nullptr, "CreateProcess"},
  773. {0x7A, nullptr, "StartProcess"},
  774. {0x7B, nullptr, "TerminateProcess"},
  775. {0x7C, nullptr, "GetProcessInfo"},
  776. {0x7D, nullptr, "CreateResourceLimit"},
  777. {0x7E, nullptr, "SetResourceLimitLimitValue"},
  778. {0x7F, nullptr, "CallSecureMonitor"},
  779. };
  780. static const FunctionDef* GetSVCInfo(u32 func_num) {
  781. if (func_num >= std::size(SVC_Table)) {
  782. NGLOG_ERROR(Kernel_SVC, "Unknown svc=0x{:02X}", func_num);
  783. return nullptr;
  784. }
  785. return &SVC_Table[func_num];
  786. }
  787. MICROPROFILE_DEFINE(Kernel_SVC, "Kernel", "SVC", MP_RGB(70, 200, 70));
  788. void CallSVC(u32 immediate) {
  789. MICROPROFILE_SCOPE(Kernel_SVC);
  790. // Lock the global kernel mutex when we enter the kernel HLE.
  791. std::lock_guard<std::recursive_mutex> lock(HLE::g_hle_lock);
  792. const FunctionDef* info = GetSVCInfo(immediate);
  793. if (info) {
  794. if (info->func) {
  795. info->func();
  796. } else {
  797. NGLOG_CRITICAL(Kernel_SVC, "Unimplemented SVC function {}(..)", info->name);
  798. }
  799. } else {
  800. NGLOG_CRITICAL(Kernel_SVC, "Unknown SVC function 0x{:X}", immediate);
  801. }
  802. }
  803. } // namespace Kernel