svc.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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 <mutex>
  8. #include <vector>
  9. #include "common/assert.h"
  10. #include "common/logging/log.h"
  11. #include "common/microprofile.h"
  12. #include "common/string_util.h"
  13. #include "core/arm/exclusive_monitor.h"
  14. #include "core/core.h"
  15. #include "core/core_cpu.h"
  16. #include "core/core_timing.h"
  17. #include "core/hle/kernel/address_arbiter.h"
  18. #include "core/hle/kernel/client_port.h"
  19. #include "core/hle/kernel/client_session.h"
  20. #include "core/hle/kernel/event.h"
  21. #include "core/hle/kernel/handle_table.h"
  22. #include "core/hle/kernel/kernel.h"
  23. #include "core/hle/kernel/mutex.h"
  24. #include "core/hle/kernel/process.h"
  25. #include "core/hle/kernel/resource_limit.h"
  26. #include "core/hle/kernel/scheduler.h"
  27. #include "core/hle/kernel/shared_memory.h"
  28. #include "core/hle/kernel/svc.h"
  29. #include "core/hle/kernel/svc_wrap.h"
  30. #include "core/hle/kernel/thread.h"
  31. #include "core/hle/lock.h"
  32. #include "core/hle/result.h"
  33. #include "core/hle/service/service.h"
  34. namespace Kernel {
  35. namespace {
  36. constexpr bool Is4KBAligned(VAddr address) {
  37. return (address & 0xFFF) == 0;
  38. }
  39. // Checks if address + size is greater than the given address
  40. // This can return false if the size causes an overflow of a 64-bit type
  41. // or if the given size is zero.
  42. constexpr bool IsValidAddressRange(VAddr address, u64 size) {
  43. return address + size > address;
  44. }
  45. // Checks if a given address range lies within a larger address range.
  46. constexpr bool IsInsideAddressRange(VAddr address, u64 size, VAddr address_range_begin,
  47. VAddr address_range_end) {
  48. const VAddr end_address = address + size - 1;
  49. return address_range_begin <= address && end_address <= address_range_end - 1;
  50. }
  51. bool IsInsideAddressSpace(const VMManager& vm, VAddr address, u64 size) {
  52. return IsInsideAddressRange(address, size, vm.GetAddressSpaceBaseAddress(),
  53. vm.GetAddressSpaceEndAddress());
  54. }
  55. bool IsInsideNewMapRegion(const VMManager& vm, VAddr address, u64 size) {
  56. return IsInsideAddressRange(address, size, vm.GetNewMapRegionBaseAddress(),
  57. vm.GetNewMapRegionEndAddress());
  58. }
  59. // Helper function that performs the common sanity checks for svcMapMemory
  60. // and svcUnmapMemory. This is doable, as both functions perform their sanitizing
  61. // in the same order.
  62. ResultCode MapUnmapMemorySanityChecks(const VMManager& vm_manager, VAddr dst_addr, VAddr src_addr,
  63. u64 size) {
  64. if (!Is4KBAligned(dst_addr) || !Is4KBAligned(src_addr)) {
  65. return ERR_INVALID_ADDRESS;
  66. }
  67. if (size == 0 || !Is4KBAligned(size)) {
  68. return ERR_INVALID_SIZE;
  69. }
  70. if (!IsValidAddressRange(dst_addr, size)) {
  71. return ERR_INVALID_ADDRESS_STATE;
  72. }
  73. if (!IsValidAddressRange(src_addr, size)) {
  74. return ERR_INVALID_ADDRESS_STATE;
  75. }
  76. if (!IsInsideAddressSpace(vm_manager, src_addr, size)) {
  77. return ERR_INVALID_ADDRESS_STATE;
  78. }
  79. if (!IsInsideNewMapRegion(vm_manager, dst_addr, size)) {
  80. return ERR_INVALID_MEMORY_RANGE;
  81. }
  82. const VAddr dst_end_address = dst_addr + size;
  83. if (dst_end_address > vm_manager.GetHeapRegionBaseAddress() &&
  84. vm_manager.GetHeapRegionEndAddress() > dst_addr) {
  85. return ERR_INVALID_MEMORY_RANGE;
  86. }
  87. if (dst_end_address > vm_manager.GetMapRegionBaseAddress() &&
  88. vm_manager.GetMapRegionEndAddress() > dst_addr) {
  89. return ERR_INVALID_MEMORY_RANGE;
  90. }
  91. return RESULT_SUCCESS;
  92. }
  93. } // Anonymous namespace
  94. /// Set the process heap to a given Size. It can both extend and shrink the heap.
  95. static ResultCode SetHeapSize(VAddr* heap_addr, u64 heap_size) {
  96. LOG_TRACE(Kernel_SVC, "called, heap_size=0x{:X}", heap_size);
  97. // Size must be a multiple of 0x200000 (2MB) and be equal to or less than 4GB.
  98. if ((heap_size & 0xFFFFFFFE001FFFFF) != 0) {
  99. return ERR_INVALID_SIZE;
  100. }
  101. auto& process = *Core::CurrentProcess();
  102. const VAddr heap_base = process.VMManager().GetHeapRegionBaseAddress();
  103. CASCADE_RESULT(*heap_addr,
  104. process.HeapAllocate(heap_base, heap_size, VMAPermission::ReadWrite));
  105. return RESULT_SUCCESS;
  106. }
  107. static ResultCode SetMemoryAttribute(VAddr addr, u64 size, u32 state0, u32 state1) {
  108. LOG_WARNING(Kernel_SVC,
  109. "(STUBBED) called, addr=0x{:X}, size=0x{:X}, state0=0x{:X}, state1=0x{:X}", addr,
  110. size, state0, state1);
  111. return RESULT_SUCCESS;
  112. }
  113. /// Maps a memory range into a different range.
  114. static ResultCode MapMemory(VAddr dst_addr, VAddr src_addr, u64 size) {
  115. LOG_TRACE(Kernel_SVC, "called, dst_addr=0x{:X}, src_addr=0x{:X}, size=0x{:X}", dst_addr,
  116. src_addr, size);
  117. auto* const current_process = Core::CurrentProcess();
  118. const auto& vm_manager = current_process->VMManager();
  119. const auto result = MapUnmapMemorySanityChecks(vm_manager, dst_addr, src_addr, size);
  120. if (result != RESULT_SUCCESS) {
  121. return result;
  122. }
  123. return current_process->MirrorMemory(dst_addr, src_addr, size);
  124. }
  125. /// Unmaps a region that was previously mapped with svcMapMemory
  126. static ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size) {
  127. LOG_TRACE(Kernel_SVC, "called, dst_addr=0x{:X}, src_addr=0x{:X}, size=0x{:X}", dst_addr,
  128. src_addr, size);
  129. auto* const current_process = Core::CurrentProcess();
  130. const auto& vm_manager = current_process->VMManager();
  131. const auto result = MapUnmapMemorySanityChecks(vm_manager, dst_addr, src_addr, size);
  132. if (result != RESULT_SUCCESS) {
  133. return result;
  134. }
  135. return current_process->UnmapMemory(dst_addr, src_addr, size);
  136. }
  137. /// Connect to an OS service given the port name, returns the handle to the port to out
  138. static ResultCode ConnectToNamedPort(Handle* out_handle, VAddr port_name_address) {
  139. if (!Memory::IsValidVirtualAddress(port_name_address)) {
  140. return ERR_NOT_FOUND;
  141. }
  142. static constexpr std::size_t PortNameMaxLength = 11;
  143. // Read 1 char beyond the max allowed port name to detect names that are too long.
  144. std::string port_name = Memory::ReadCString(port_name_address, PortNameMaxLength + 1);
  145. if (port_name.size() > PortNameMaxLength) {
  146. return ERR_PORT_NAME_TOO_LONG;
  147. }
  148. LOG_TRACE(Kernel_SVC, "called port_name={}", port_name);
  149. auto& kernel = Core::System::GetInstance().Kernel();
  150. auto it = kernel.FindNamedPort(port_name);
  151. if (!kernel.IsValidNamedPort(it)) {
  152. LOG_WARNING(Kernel_SVC, "tried to connect to unknown port: {}", port_name);
  153. return ERR_NOT_FOUND;
  154. }
  155. auto client_port = it->second;
  156. SharedPtr<ClientSession> client_session;
  157. CASCADE_RESULT(client_session, client_port->Connect());
  158. // Return the client session
  159. CASCADE_RESULT(*out_handle, kernel.HandleTable().Create(client_session));
  160. return RESULT_SUCCESS;
  161. }
  162. /// Makes a blocking IPC call to an OS service.
  163. static ResultCode SendSyncRequest(Handle handle) {
  164. auto& kernel = Core::System::GetInstance().Kernel();
  165. SharedPtr<ClientSession> session = kernel.HandleTable().Get<ClientSession>(handle);
  166. if (!session) {
  167. LOG_ERROR(Kernel_SVC, "called with invalid handle=0x{:08X}", handle);
  168. return ERR_INVALID_HANDLE;
  169. }
  170. LOG_TRACE(Kernel_SVC, "called handle=0x{:08X}({})", handle, session->GetName());
  171. Core::System::GetInstance().PrepareReschedule();
  172. // TODO(Subv): svcSendSyncRequest should put the caller thread to sleep while the server
  173. // responds and cause a reschedule.
  174. return session->SendSyncRequest(GetCurrentThread());
  175. }
  176. /// Get the ID for the specified thread.
  177. static ResultCode GetThreadId(u32* thread_id, Handle thread_handle) {
  178. LOG_TRACE(Kernel_SVC, "called thread=0x{:08X}", thread_handle);
  179. auto& kernel = Core::System::GetInstance().Kernel();
  180. const SharedPtr<Thread> thread = kernel.HandleTable().Get<Thread>(thread_handle);
  181. if (!thread) {
  182. return ERR_INVALID_HANDLE;
  183. }
  184. *thread_id = thread->GetThreadID();
  185. return RESULT_SUCCESS;
  186. }
  187. /// Get the ID of the specified process
  188. static ResultCode GetProcessId(u32* process_id, Handle process_handle) {
  189. LOG_TRACE(Kernel_SVC, "called process=0x{:08X}", process_handle);
  190. auto& kernel = Core::System::GetInstance().Kernel();
  191. const SharedPtr<Process> process = kernel.HandleTable().Get<Process>(process_handle);
  192. if (!process) {
  193. return ERR_INVALID_HANDLE;
  194. }
  195. *process_id = process->GetProcessID();
  196. return RESULT_SUCCESS;
  197. }
  198. /// Default thread wakeup callback for WaitSynchronization
  199. static bool DefaultThreadWakeupCallback(ThreadWakeupReason reason, SharedPtr<Thread> thread,
  200. SharedPtr<WaitObject> object, std::size_t index) {
  201. ASSERT(thread->GetStatus() == ThreadStatus::WaitSynchAny);
  202. if (reason == ThreadWakeupReason::Timeout) {
  203. thread->SetWaitSynchronizationResult(RESULT_TIMEOUT);
  204. return true;
  205. }
  206. ASSERT(reason == ThreadWakeupReason::Signal);
  207. thread->SetWaitSynchronizationResult(RESULT_SUCCESS);
  208. thread->SetWaitSynchronizationOutput(static_cast<u32>(index));
  209. return true;
  210. };
  211. /// Wait for the given handles to synchronize, timeout after the specified nanoseconds
  212. static ResultCode WaitSynchronization(Handle* index, VAddr handles_address, u64 handle_count,
  213. s64 nano_seconds) {
  214. LOG_TRACE(Kernel_SVC, "called handles_address=0x{:X}, handle_count={}, nano_seconds={}",
  215. handles_address, handle_count, nano_seconds);
  216. if (!Memory::IsValidVirtualAddress(handles_address))
  217. return ERR_INVALID_POINTER;
  218. static constexpr u64 MaxHandles = 0x40;
  219. if (handle_count > MaxHandles)
  220. return ResultCode(ErrorModule::Kernel, ErrCodes::TooLarge);
  221. auto* const thread = GetCurrentThread();
  222. using ObjectPtr = Thread::ThreadWaitObjects::value_type;
  223. Thread::ThreadWaitObjects objects(handle_count);
  224. auto& kernel = Core::System::GetInstance().Kernel();
  225. for (u64 i = 0; i < handle_count; ++i) {
  226. const Handle handle = Memory::Read32(handles_address + i * sizeof(Handle));
  227. const auto object = kernel.HandleTable().Get<WaitObject>(handle);
  228. if (object == nullptr) {
  229. return ERR_INVALID_HANDLE;
  230. }
  231. objects[i] = object;
  232. }
  233. // Find the first object that is acquirable in the provided list of objects
  234. auto itr = std::find_if(objects.begin(), objects.end(), [thread](const ObjectPtr& object) {
  235. return !object->ShouldWait(thread);
  236. });
  237. if (itr != objects.end()) {
  238. // We found a ready object, acquire it and set the result value
  239. WaitObject* object = itr->get();
  240. object->Acquire(thread);
  241. *index = static_cast<s32>(std::distance(objects.begin(), itr));
  242. return RESULT_SUCCESS;
  243. }
  244. // No objects were ready to be acquired, prepare to suspend the thread.
  245. // If a timeout value of 0 was provided, just return the Timeout error code instead of
  246. // suspending the thread.
  247. if (nano_seconds == 0)
  248. return RESULT_TIMEOUT;
  249. for (auto& object : objects)
  250. object->AddWaitingThread(thread);
  251. thread->SetWaitObjects(std::move(objects));
  252. thread->SetStatus(ThreadStatus::WaitSynchAny);
  253. // Create an event to wake the thread up after the specified nanosecond delay has passed
  254. thread->WakeAfterDelay(nano_seconds);
  255. thread->SetWakeupCallback(DefaultThreadWakeupCallback);
  256. Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule();
  257. return RESULT_TIMEOUT;
  258. }
  259. /// Resumes a thread waiting on WaitSynchronization
  260. static ResultCode CancelSynchronization(Handle thread_handle) {
  261. LOG_TRACE(Kernel_SVC, "called thread=0x{:X}", thread_handle);
  262. auto& kernel = Core::System::GetInstance().Kernel();
  263. const SharedPtr<Thread> thread = kernel.HandleTable().Get<Thread>(thread_handle);
  264. if (!thread) {
  265. return ERR_INVALID_HANDLE;
  266. }
  267. ASSERT(thread->GetStatus() == ThreadStatus::WaitSynchAny);
  268. thread->SetWaitSynchronizationResult(
  269. ResultCode(ErrorModule::Kernel, ErrCodes::SynchronizationCanceled));
  270. thread->ResumeFromWait();
  271. return RESULT_SUCCESS;
  272. }
  273. /// Attempts to locks a mutex, creating it if it does not already exist
  274. static ResultCode ArbitrateLock(Handle holding_thread_handle, VAddr mutex_addr,
  275. Handle requesting_thread_handle) {
  276. LOG_TRACE(Kernel_SVC,
  277. "called holding_thread_handle=0x{:08X}, mutex_addr=0x{:X}, "
  278. "requesting_current_thread_handle=0x{:08X}",
  279. holding_thread_handle, mutex_addr, requesting_thread_handle);
  280. if (Memory::IsKernelVirtualAddress(mutex_addr)) {
  281. return ERR_INVALID_ADDRESS_STATE;
  282. }
  283. auto& handle_table = Core::System::GetInstance().Kernel().HandleTable();
  284. return Mutex::TryAcquire(handle_table, mutex_addr, holding_thread_handle,
  285. requesting_thread_handle);
  286. }
  287. /// Unlock a mutex
  288. static ResultCode ArbitrateUnlock(VAddr mutex_addr) {
  289. LOG_TRACE(Kernel_SVC, "called mutex_addr=0x{:X}", mutex_addr);
  290. if (Memory::IsKernelVirtualAddress(mutex_addr)) {
  291. return ERR_INVALID_ADDRESS_STATE;
  292. }
  293. return Mutex::Release(mutex_addr);
  294. }
  295. struct BreakReason {
  296. union {
  297. u32 raw;
  298. BitField<31, 1, u32> signal_debugger;
  299. };
  300. };
  301. /// Break program execution
  302. static void Break(u32 reason, u64 info1, u64 info2) {
  303. BreakReason break_reason{reason};
  304. if (break_reason.signal_debugger) {
  305. LOG_ERROR(
  306. Debug_Emulated,
  307. "Emulated program broke execution! reason=0x{:016X}, info1=0x{:016X}, info2=0x{:016X}",
  308. reason, info1, info2);
  309. } else {
  310. LOG_CRITICAL(
  311. Debug_Emulated,
  312. "Emulated program broke execution! reason=0x{:016X}, info1=0x{:016X}, info2=0x{:016X}",
  313. reason, info1, info2);
  314. ASSERT(false);
  315. Core::CurrentProcess()->PrepareForTermination();
  316. // Kill the current thread
  317. GetCurrentThread()->Stop();
  318. Core::System::GetInstance().PrepareReschedule();
  319. }
  320. }
  321. /// Used to output a message on a debug hardware unit - does nothing on a retail unit
  322. static void OutputDebugString(VAddr address, u64 len) {
  323. if (len == 0) {
  324. return;
  325. }
  326. std::string str(len, '\0');
  327. Memory::ReadBlock(address, str.data(), str.size());
  328. LOG_DEBUG(Debug_Emulated, "{}", str);
  329. }
  330. /// Gets system/memory information for the current process
  331. static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) {
  332. LOG_TRACE(Kernel_SVC, "called info_id=0x{:X}, info_sub_id=0x{:X}, handle=0x{:08X}", info_id,
  333. info_sub_id, handle);
  334. const auto* current_process = Core::CurrentProcess();
  335. const auto& vm_manager = current_process->VMManager();
  336. switch (static_cast<GetInfoType>(info_id)) {
  337. case GetInfoType::AllowedCpuIdBitmask:
  338. *result = current_process->GetAllowedProcessorMask();
  339. break;
  340. case GetInfoType::AllowedThreadPrioBitmask:
  341. *result = current_process->GetAllowedThreadPriorityMask();
  342. break;
  343. case GetInfoType::MapRegionBaseAddr:
  344. *result = vm_manager.GetMapRegionBaseAddress();
  345. break;
  346. case GetInfoType::MapRegionSize:
  347. *result = vm_manager.GetMapRegionSize();
  348. break;
  349. case GetInfoType::HeapRegionBaseAddr:
  350. *result = vm_manager.GetHeapRegionBaseAddress();
  351. break;
  352. case GetInfoType::HeapRegionSize:
  353. *result = vm_manager.GetHeapRegionSize();
  354. break;
  355. case GetInfoType::TotalMemoryUsage:
  356. *result = vm_manager.GetTotalMemoryUsage();
  357. break;
  358. case GetInfoType::TotalHeapUsage:
  359. *result = vm_manager.GetTotalHeapUsage();
  360. break;
  361. case GetInfoType::IsCurrentProcessBeingDebugged:
  362. *result = 0;
  363. break;
  364. case GetInfoType::RandomEntropy:
  365. *result = 0;
  366. break;
  367. case GetInfoType::AddressSpaceBaseAddr:
  368. *result = vm_manager.GetCodeRegionBaseAddress();
  369. break;
  370. case GetInfoType::AddressSpaceSize: {
  371. const u64 width = vm_manager.GetAddressSpaceWidth();
  372. switch (width) {
  373. case 32:
  374. *result = 0xFFE00000;
  375. break;
  376. case 36:
  377. *result = 0xFF8000000;
  378. break;
  379. case 39:
  380. *result = 0x7FF8000000;
  381. break;
  382. }
  383. break;
  384. }
  385. case GetInfoType::NewMapRegionBaseAddr:
  386. *result = vm_manager.GetNewMapRegionBaseAddress();
  387. break;
  388. case GetInfoType::NewMapRegionSize:
  389. *result = vm_manager.GetNewMapRegionSize();
  390. break;
  391. case GetInfoType::IsVirtualAddressMemoryEnabled:
  392. *result = current_process->IsVirtualMemoryEnabled();
  393. break;
  394. case GetInfoType::TitleId:
  395. *result = current_process->GetTitleID();
  396. break;
  397. case GetInfoType::PrivilegedProcessId:
  398. LOG_WARNING(Kernel_SVC,
  399. "(STUBBED) Attempted to query privileged process id bounds, returned 0");
  400. *result = 0;
  401. break;
  402. case GetInfoType::UserExceptionContextAddr:
  403. LOG_WARNING(Kernel_SVC,
  404. "(STUBBED) Attempted to query user exception context address, returned 0");
  405. *result = 0;
  406. break;
  407. default:
  408. UNIMPLEMENTED();
  409. }
  410. return RESULT_SUCCESS;
  411. }
  412. /// Sets the thread activity
  413. static ResultCode SetThreadActivity(Handle handle, u32 unknown) {
  414. LOG_WARNING(Kernel_SVC, "(STUBBED) called, handle=0x{:08X}, unknown=0x{:08X}", handle, unknown);
  415. return RESULT_SUCCESS;
  416. }
  417. /// Gets the thread context
  418. static ResultCode GetThreadContext(VAddr thread_context, Handle handle) {
  419. LOG_DEBUG(Kernel_SVC, "called, context=0x{:08X}, thread=0x{:X}", thread_context, handle);
  420. auto& kernel = Core::System::GetInstance().Kernel();
  421. const SharedPtr<Thread> thread = kernel.HandleTable().Get<Thread>(handle);
  422. if (!thread) {
  423. return ERR_INVALID_HANDLE;
  424. }
  425. const auto* current_process = Core::CurrentProcess();
  426. if (thread->GetOwnerProcess() != current_process) {
  427. return ERR_INVALID_HANDLE;
  428. }
  429. if (thread == GetCurrentThread()) {
  430. return ERR_ALREADY_REGISTERED;
  431. }
  432. Core::ARM_Interface::ThreadContext ctx = thread->GetContext();
  433. // Mask away mode bits, interrupt bits, IL bit, and other reserved bits.
  434. ctx.pstate &= 0xFF0FFE20;
  435. // If 64-bit, we can just write the context registers directly and we're good.
  436. // However, if 32-bit, we have to ensure some registers are zeroed out.
  437. if (!current_process->Is64BitProcess()) {
  438. std::fill(ctx.cpu_registers.begin() + 15, ctx.cpu_registers.end(), 0);
  439. std::fill(ctx.vector_registers.begin() + 16, ctx.vector_registers.end(), u128{});
  440. }
  441. Memory::WriteBlock(thread_context, &ctx, sizeof(ctx));
  442. return RESULT_SUCCESS;
  443. }
  444. /// Gets the priority for the specified thread
  445. static ResultCode GetThreadPriority(u32* priority, Handle handle) {
  446. auto& kernel = Core::System::GetInstance().Kernel();
  447. const SharedPtr<Thread> thread = kernel.HandleTable().Get<Thread>(handle);
  448. if (!thread)
  449. return ERR_INVALID_HANDLE;
  450. *priority = thread->GetPriority();
  451. return RESULT_SUCCESS;
  452. }
  453. /// Sets the priority for the specified thread
  454. static ResultCode SetThreadPriority(Handle handle, u32 priority) {
  455. if (priority > THREADPRIO_LOWEST) {
  456. return ERR_INVALID_THREAD_PRIORITY;
  457. }
  458. auto& kernel = Core::System::GetInstance().Kernel();
  459. SharedPtr<Thread> thread = kernel.HandleTable().Get<Thread>(handle);
  460. if (!thread)
  461. return ERR_INVALID_HANDLE;
  462. // Note: The kernel uses the current process's resource limit instead of
  463. // the one from the thread owner's resource limit.
  464. const ResourceLimit& resource_limit = Core::CurrentProcess()->GetResourceLimit();
  465. if (resource_limit.GetMaxResourceValue(ResourceType::Priority) > priority) {
  466. return ERR_NOT_AUTHORIZED;
  467. }
  468. thread->SetPriority(priority);
  469. Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule();
  470. return RESULT_SUCCESS;
  471. }
  472. /// Get which CPU core is executing the current thread
  473. static u32 GetCurrentProcessorNumber() {
  474. LOG_TRACE(Kernel_SVC, "called");
  475. return GetCurrentThread()->GetProcessorID();
  476. }
  477. static ResultCode MapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 size,
  478. u32 permissions) {
  479. LOG_TRACE(Kernel_SVC,
  480. "called, shared_memory_handle=0x{:X}, addr=0x{:X}, size=0x{:X}, permissions=0x{:08X}",
  481. shared_memory_handle, addr, size, permissions);
  482. if (!Is4KBAligned(addr)) {
  483. return ERR_INVALID_ADDRESS;
  484. }
  485. if (size == 0 || !Is4KBAligned(size)) {
  486. return ERR_INVALID_SIZE;
  487. }
  488. const auto permissions_type = static_cast<MemoryPermission>(permissions);
  489. if (permissions_type != MemoryPermission::Read &&
  490. permissions_type != MemoryPermission::ReadWrite) {
  491. LOG_ERROR(Kernel_SVC, "Invalid permissions=0x{:08X}", permissions);
  492. return ERR_INVALID_MEMORY_PERMISSIONS;
  493. }
  494. auto& kernel = Core::System::GetInstance().Kernel();
  495. auto shared_memory = kernel.HandleTable().Get<SharedMemory>(shared_memory_handle);
  496. if (!shared_memory) {
  497. return ERR_INVALID_HANDLE;
  498. }
  499. return shared_memory->Map(Core::CurrentProcess(), addr, permissions_type,
  500. MemoryPermission::DontCare);
  501. }
  502. static ResultCode UnmapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 size) {
  503. LOG_WARNING(Kernel_SVC, "called, shared_memory_handle=0x{:08X}, addr=0x{:X}, size=0x{:X}",
  504. shared_memory_handle, addr, size);
  505. if (!Is4KBAligned(addr)) {
  506. return ERR_INVALID_ADDRESS;
  507. }
  508. if (size == 0 || !Is4KBAligned(size)) {
  509. return ERR_INVALID_SIZE;
  510. }
  511. auto& kernel = Core::System::GetInstance().Kernel();
  512. auto shared_memory = kernel.HandleTable().Get<SharedMemory>(shared_memory_handle);
  513. return shared_memory->Unmap(Core::CurrentProcess(), addr);
  514. }
  515. /// Query process memory
  516. static ResultCode QueryProcessMemory(MemoryInfo* memory_info, PageInfo* /*page_info*/,
  517. Handle process_handle, u64 addr) {
  518. auto& kernel = Core::System::GetInstance().Kernel();
  519. SharedPtr<Process> process = kernel.HandleTable().Get<Process>(process_handle);
  520. if (!process) {
  521. return ERR_INVALID_HANDLE;
  522. }
  523. auto vma = process->VMManager().FindVMA(addr);
  524. memory_info->attributes = 0;
  525. if (vma == Core::CurrentProcess()->VMManager().vma_map.end()) {
  526. memory_info->base_address = 0;
  527. memory_info->permission = static_cast<u32>(VMAPermission::None);
  528. memory_info->size = 0;
  529. memory_info->type = static_cast<u32>(MemoryState::Unmapped);
  530. } else {
  531. memory_info->base_address = vma->second.base;
  532. memory_info->permission = static_cast<u32>(vma->second.permissions);
  533. memory_info->size = vma->second.size;
  534. memory_info->type = static_cast<u32>(vma->second.meminfo_state);
  535. }
  536. LOG_TRACE(Kernel_SVC, "called process=0x{:08X} addr={:X}", process_handle, addr);
  537. return RESULT_SUCCESS;
  538. }
  539. /// Query memory
  540. static ResultCode QueryMemory(MemoryInfo* memory_info, PageInfo* page_info, VAddr addr) {
  541. LOG_TRACE(Kernel_SVC, "called, addr={:X}", addr);
  542. return QueryProcessMemory(memory_info, page_info, CurrentProcess, addr);
  543. }
  544. /// Exits the current process
  545. static void ExitProcess() {
  546. auto* current_process = Core::CurrentProcess();
  547. LOG_INFO(Kernel_SVC, "Process {} exiting", current_process->GetProcessID());
  548. ASSERT_MSG(current_process->GetStatus() == ProcessStatus::Running,
  549. "Process has already exited");
  550. current_process->PrepareForTermination();
  551. // Kill the current thread
  552. GetCurrentThread()->Stop();
  553. Core::System::GetInstance().PrepareReschedule();
  554. }
  555. /// Creates a new thread
  556. static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, VAddr stack_top,
  557. u32 priority, s32 processor_id) {
  558. std::string name = fmt::format("thread-{:X}", entry_point);
  559. if (priority > THREADPRIO_LOWEST) {
  560. return ERR_INVALID_THREAD_PRIORITY;
  561. }
  562. const ResourceLimit& resource_limit = Core::CurrentProcess()->GetResourceLimit();
  563. if (resource_limit.GetMaxResourceValue(ResourceType::Priority) > priority) {
  564. return ERR_NOT_AUTHORIZED;
  565. }
  566. if (processor_id == THREADPROCESSORID_DEFAULT) {
  567. // Set the target CPU to the one specified in the process' exheader.
  568. processor_id = Core::CurrentProcess()->GetDefaultProcessorID();
  569. ASSERT(processor_id != THREADPROCESSORID_DEFAULT);
  570. }
  571. switch (processor_id) {
  572. case THREADPROCESSORID_0:
  573. case THREADPROCESSORID_1:
  574. case THREADPROCESSORID_2:
  575. case THREADPROCESSORID_3:
  576. break;
  577. default:
  578. LOG_ERROR(Kernel_SVC, "Invalid thread processor ID: {}", processor_id);
  579. return ERR_INVALID_PROCESSOR_ID;
  580. }
  581. auto& kernel = Core::System::GetInstance().Kernel();
  582. CASCADE_RESULT(SharedPtr<Thread> thread,
  583. Thread::Create(kernel, name, entry_point, priority, arg, processor_id, stack_top,
  584. *Core::CurrentProcess()));
  585. const auto new_guest_handle = kernel.HandleTable().Create(thread);
  586. if (new_guest_handle.Failed()) {
  587. return new_guest_handle.Code();
  588. }
  589. thread->SetGuestHandle(*new_guest_handle);
  590. *out_handle = *new_guest_handle;
  591. Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule();
  592. LOG_TRACE(Kernel_SVC,
  593. "called entrypoint=0x{:08X} ({}), arg=0x{:08X}, stacktop=0x{:08X}, "
  594. "threadpriority=0x{:08X}, processorid=0x{:08X} : created handle=0x{:08X}",
  595. entry_point, name, arg, stack_top, priority, processor_id, *out_handle);
  596. return RESULT_SUCCESS;
  597. }
  598. /// Starts the thread for the provided handle
  599. static ResultCode StartThread(Handle thread_handle) {
  600. LOG_TRACE(Kernel_SVC, "called thread=0x{:08X}", thread_handle);
  601. auto& kernel = Core::System::GetInstance().Kernel();
  602. const SharedPtr<Thread> thread = kernel.HandleTable().Get<Thread>(thread_handle);
  603. if (!thread) {
  604. return ERR_INVALID_HANDLE;
  605. }
  606. ASSERT(thread->GetStatus() == ThreadStatus::Dormant);
  607. thread->ResumeFromWait();
  608. Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule();
  609. return RESULT_SUCCESS;
  610. }
  611. /// Called when a thread exits
  612. static void ExitThread() {
  613. LOG_TRACE(Kernel_SVC, "called, pc=0x{:08X}", Core::CurrentArmInterface().GetPC());
  614. ExitCurrentThread();
  615. Core::System::GetInstance().PrepareReschedule();
  616. }
  617. /// Sleep the current thread
  618. static void SleepThread(s64 nanoseconds) {
  619. LOG_TRACE(Kernel_SVC, "called nanoseconds={}", nanoseconds);
  620. // Don't attempt to yield execution if there are no available threads to run,
  621. // this way we avoid a useless reschedule to the idle thread.
  622. if (nanoseconds == 0 && !Core::System::GetInstance().CurrentScheduler().HaveReadyThreads())
  623. return;
  624. // Sleep current thread and check for next thread to schedule
  625. WaitCurrentThread_Sleep();
  626. // Create an event to wake the thread up after the specified nanosecond delay has passed
  627. GetCurrentThread()->WakeAfterDelay(nanoseconds);
  628. Core::System::GetInstance().PrepareReschedule();
  629. }
  630. /// Wait process wide key atomic
  631. static ResultCode WaitProcessWideKeyAtomic(VAddr mutex_addr, VAddr condition_variable_addr,
  632. Handle thread_handle, s64 nano_seconds) {
  633. LOG_TRACE(
  634. Kernel_SVC,
  635. "called mutex_addr={:X}, condition_variable_addr={:X}, thread_handle=0x{:08X}, timeout={}",
  636. mutex_addr, condition_variable_addr, thread_handle, nano_seconds);
  637. auto& kernel = Core::System::GetInstance().Kernel();
  638. SharedPtr<Thread> thread = kernel.HandleTable().Get<Thread>(thread_handle);
  639. ASSERT(thread);
  640. CASCADE_CODE(Mutex::Release(mutex_addr));
  641. SharedPtr<Thread> current_thread = GetCurrentThread();
  642. current_thread->SetCondVarWaitAddress(condition_variable_addr);
  643. current_thread->SetMutexWaitAddress(mutex_addr);
  644. current_thread->SetWaitHandle(thread_handle);
  645. current_thread->SetStatus(ThreadStatus::WaitMutex);
  646. current_thread->InvalidateWakeupCallback();
  647. current_thread->WakeAfterDelay(nano_seconds);
  648. // Note: Deliberately don't attempt to inherit the lock owner's priority.
  649. Core::System::GetInstance().CpuCore(current_thread->GetProcessorID()).PrepareReschedule();
  650. return RESULT_SUCCESS;
  651. }
  652. /// Signal process wide key
  653. static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target) {
  654. LOG_TRACE(Kernel_SVC, "called, condition_variable_addr=0x{:X}, target=0x{:08X}",
  655. condition_variable_addr, target);
  656. const auto RetrieveWaitingThreads = [](std::size_t core_index,
  657. std::vector<SharedPtr<Thread>>& waiting_threads,
  658. VAddr condvar_addr) {
  659. const auto& scheduler = Core::System::GetInstance().Scheduler(core_index);
  660. const auto& thread_list = scheduler->GetThreadList();
  661. for (const auto& thread : thread_list) {
  662. if (thread->GetCondVarWaitAddress() == condvar_addr)
  663. waiting_threads.push_back(thread);
  664. }
  665. };
  666. // Retrieve a list of all threads that are waiting for this condition variable.
  667. std::vector<SharedPtr<Thread>> waiting_threads;
  668. RetrieveWaitingThreads(0, waiting_threads, condition_variable_addr);
  669. RetrieveWaitingThreads(1, waiting_threads, condition_variable_addr);
  670. RetrieveWaitingThreads(2, waiting_threads, condition_variable_addr);
  671. RetrieveWaitingThreads(3, waiting_threads, condition_variable_addr);
  672. // Sort them by priority, such that the highest priority ones come first.
  673. std::sort(waiting_threads.begin(), waiting_threads.end(),
  674. [](const SharedPtr<Thread>& lhs, const SharedPtr<Thread>& rhs) {
  675. return lhs->GetPriority() < rhs->GetPriority();
  676. });
  677. // Only process up to 'target' threads, unless 'target' is -1, in which case process
  678. // them all.
  679. std::size_t last = waiting_threads.size();
  680. if (target != -1)
  681. last = target;
  682. // If there are no threads waiting on this condition variable, just exit
  683. if (last > waiting_threads.size())
  684. return RESULT_SUCCESS;
  685. for (std::size_t index = 0; index < last; ++index) {
  686. auto& thread = waiting_threads[index];
  687. ASSERT(thread->GetCondVarWaitAddress() == condition_variable_addr);
  688. std::size_t current_core = Core::System::GetInstance().CurrentCoreIndex();
  689. auto& monitor = Core::System::GetInstance().Monitor();
  690. // Atomically read the value of the mutex.
  691. u32 mutex_val = 0;
  692. do {
  693. monitor.SetExclusive(current_core, thread->GetMutexWaitAddress());
  694. // If the mutex is not yet acquired, acquire it.
  695. mutex_val = Memory::Read32(thread->GetMutexWaitAddress());
  696. if (mutex_val != 0) {
  697. monitor.ClearExclusive();
  698. break;
  699. }
  700. } while (!monitor.ExclusiveWrite32(current_core, thread->GetMutexWaitAddress(),
  701. thread->GetWaitHandle()));
  702. if (mutex_val == 0) {
  703. // We were able to acquire the mutex, resume this thread.
  704. ASSERT(thread->GetStatus() == ThreadStatus::WaitMutex);
  705. thread->ResumeFromWait();
  706. auto* const lock_owner = thread->GetLockOwner();
  707. if (lock_owner != nullptr) {
  708. lock_owner->RemoveMutexWaiter(thread);
  709. }
  710. thread->SetLockOwner(nullptr);
  711. thread->SetMutexWaitAddress(0);
  712. thread->SetCondVarWaitAddress(0);
  713. thread->SetWaitHandle(0);
  714. } else {
  715. // Atomically signal that the mutex now has a waiting thread.
  716. do {
  717. monitor.SetExclusive(current_core, thread->GetMutexWaitAddress());
  718. // Ensure that the mutex value is still what we expect.
  719. u32 value = Memory::Read32(thread->GetMutexWaitAddress());
  720. // TODO(Subv): When this happens, the kernel just clears the exclusive state and
  721. // retries the initial read for this thread.
  722. ASSERT_MSG(mutex_val == value, "Unhandled synchronization primitive case");
  723. } while (!monitor.ExclusiveWrite32(current_core, thread->GetMutexWaitAddress(),
  724. mutex_val | Mutex::MutexHasWaitersFlag));
  725. // The mutex is already owned by some other thread, make this thread wait on it.
  726. auto& kernel = Core::System::GetInstance().Kernel();
  727. Handle owner_handle = static_cast<Handle>(mutex_val & Mutex::MutexOwnerMask);
  728. auto owner = kernel.HandleTable().Get<Thread>(owner_handle);
  729. ASSERT(owner);
  730. ASSERT(thread->GetStatus() == ThreadStatus::WaitMutex);
  731. thread->InvalidateWakeupCallback();
  732. owner->AddMutexWaiter(thread);
  733. Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule();
  734. }
  735. }
  736. return RESULT_SUCCESS;
  737. }
  738. // Wait for an address (via Address Arbiter)
  739. static ResultCode WaitForAddress(VAddr address, u32 type, s32 value, s64 timeout) {
  740. LOG_WARNING(Kernel_SVC, "called, address=0x{:X}, type=0x{:X}, value=0x{:X}, timeout={}",
  741. address, type, value, timeout);
  742. // If the passed address is a kernel virtual address, return invalid memory state.
  743. if (Memory::IsKernelVirtualAddress(address)) {
  744. return ERR_INVALID_ADDRESS_STATE;
  745. }
  746. // If the address is not properly aligned to 4 bytes, return invalid address.
  747. if (address % sizeof(u32) != 0) {
  748. return ERR_INVALID_ADDRESS;
  749. }
  750. switch (static_cast<AddressArbiter::ArbitrationType>(type)) {
  751. case AddressArbiter::ArbitrationType::WaitIfLessThan:
  752. return AddressArbiter::WaitForAddressIfLessThan(address, value, timeout, false);
  753. case AddressArbiter::ArbitrationType::DecrementAndWaitIfLessThan:
  754. return AddressArbiter::WaitForAddressIfLessThan(address, value, timeout, true);
  755. case AddressArbiter::ArbitrationType::WaitIfEqual:
  756. return AddressArbiter::WaitForAddressIfEqual(address, value, timeout);
  757. default:
  758. return ERR_INVALID_ENUM_VALUE;
  759. }
  760. }
  761. // Signals to an address (via Address Arbiter)
  762. static ResultCode SignalToAddress(VAddr address, u32 type, s32 value, s32 num_to_wake) {
  763. LOG_WARNING(Kernel_SVC, "called, address=0x{:X}, type=0x{:X}, value=0x{:X}, num_to_wake=0x{:X}",
  764. address, type, value, num_to_wake);
  765. // If the passed address is a kernel virtual address, return invalid memory state.
  766. if (Memory::IsKernelVirtualAddress(address)) {
  767. return ERR_INVALID_ADDRESS_STATE;
  768. }
  769. // If the address is not properly aligned to 4 bytes, return invalid address.
  770. if (address % sizeof(u32) != 0) {
  771. return ERR_INVALID_ADDRESS;
  772. }
  773. switch (static_cast<AddressArbiter::SignalType>(type)) {
  774. case AddressArbiter::SignalType::Signal:
  775. return AddressArbiter::SignalToAddress(address, num_to_wake);
  776. case AddressArbiter::SignalType::IncrementAndSignalIfEqual:
  777. return AddressArbiter::IncrementAndSignalToAddressIfEqual(address, value, num_to_wake);
  778. case AddressArbiter::SignalType::ModifyByWaitingCountAndSignalIfEqual:
  779. return AddressArbiter::ModifyByWaitingCountAndSignalToAddressIfEqual(address, value,
  780. num_to_wake);
  781. default:
  782. return ERR_INVALID_ENUM_VALUE;
  783. }
  784. }
  785. /// This returns the total CPU ticks elapsed since the CPU was powered-on
  786. static u64 GetSystemTick() {
  787. const u64 result{CoreTiming::GetTicks()};
  788. // Advance time to defeat dumb games that busy-wait for the frame to end.
  789. CoreTiming::AddTicks(400);
  790. return result;
  791. }
  792. /// Close a handle
  793. static ResultCode CloseHandle(Handle handle) {
  794. LOG_TRACE(Kernel_SVC, "Closing handle 0x{:08X}", handle);
  795. auto& kernel = Core::System::GetInstance().Kernel();
  796. return kernel.HandleTable().Close(handle);
  797. }
  798. /// Reset an event
  799. static ResultCode ResetSignal(Handle handle) {
  800. LOG_WARNING(Kernel_SVC, "(STUBBED) called handle 0x{:08X}", handle);
  801. auto& kernel = Core::System::GetInstance().Kernel();
  802. auto event = kernel.HandleTable().Get<Event>(handle);
  803. ASSERT(event != nullptr);
  804. event->Clear();
  805. return RESULT_SUCCESS;
  806. }
  807. /// Creates a TransferMemory object
  808. static ResultCode CreateTransferMemory(Handle* handle, VAddr addr, u64 size, u32 permissions) {
  809. LOG_WARNING(Kernel_SVC, "(STUBBED) called addr=0x{:X}, size=0x{:X}, perms=0x{:08X}", addr, size,
  810. permissions);
  811. *handle = 0;
  812. return RESULT_SUCCESS;
  813. }
  814. static ResultCode GetThreadCoreMask(Handle thread_handle, u32* core, u64* mask) {
  815. LOG_TRACE(Kernel_SVC, "called, handle=0x{:08X}", thread_handle);
  816. auto& kernel = Core::System::GetInstance().Kernel();
  817. const SharedPtr<Thread> thread = kernel.HandleTable().Get<Thread>(thread_handle);
  818. if (!thread) {
  819. return ERR_INVALID_HANDLE;
  820. }
  821. *core = thread->GetIdealCore();
  822. *mask = thread->GetAffinityMask();
  823. return RESULT_SUCCESS;
  824. }
  825. static ResultCode SetThreadCoreMask(Handle thread_handle, u32 core, u64 mask) {
  826. LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, mask=0x{:16X}, core=0x{:X}", thread_handle,
  827. mask, core);
  828. auto& kernel = Core::System::GetInstance().Kernel();
  829. const SharedPtr<Thread> thread = kernel.HandleTable().Get<Thread>(thread_handle);
  830. if (!thread) {
  831. return ERR_INVALID_HANDLE;
  832. }
  833. if (core == static_cast<u32>(THREADPROCESSORID_DEFAULT)) {
  834. const u8 default_processor_id = thread->GetOwnerProcess()->GetDefaultProcessorID();
  835. ASSERT(default_processor_id != static_cast<u8>(THREADPROCESSORID_DEFAULT));
  836. // Set the target CPU to the one specified in the process' exheader.
  837. core = default_processor_id;
  838. mask = 1ULL << core;
  839. }
  840. if (mask == 0) {
  841. return ResultCode(ErrorModule::Kernel, ErrCodes::InvalidCombination);
  842. }
  843. /// This value is used to only change the affinity mask without changing the current ideal core.
  844. static constexpr u32 OnlyChangeMask = static_cast<u32>(-3);
  845. if (core == OnlyChangeMask) {
  846. core = thread->GetIdealCore();
  847. } else if (core >= Core::NUM_CPU_CORES && core != static_cast<u32>(-1)) {
  848. return ResultCode(ErrorModule::Kernel, ErrCodes::InvalidProcessorId);
  849. }
  850. // Error out if the input core isn't enabled in the input mask.
  851. if (core < Core::NUM_CPU_CORES && (mask & (1ull << core)) == 0) {
  852. return ResultCode(ErrorModule::Kernel, ErrCodes::InvalidCombination);
  853. }
  854. thread->ChangeCore(core, mask);
  855. return RESULT_SUCCESS;
  856. }
  857. static ResultCode CreateSharedMemory(Handle* handle, u64 size, u32 local_permissions,
  858. u32 remote_permissions) {
  859. LOG_TRACE(Kernel_SVC, "called, size=0x{:X}, localPerms=0x{:08X}, remotePerms=0x{:08X}", size,
  860. local_permissions, remote_permissions);
  861. // Size must be a multiple of 4KB and be less than or equal to
  862. // approx. 8 GB (actually (1GB - 512B) * 8)
  863. if (size == 0 || (size & 0xFFFFFFFE00000FFF) != 0) {
  864. return ERR_INVALID_SIZE;
  865. }
  866. const auto local_perms = static_cast<MemoryPermission>(local_permissions);
  867. if (local_perms != MemoryPermission::Read && local_perms != MemoryPermission::ReadWrite) {
  868. return ERR_INVALID_MEMORY_PERMISSIONS;
  869. }
  870. const auto remote_perms = static_cast<MemoryPermission>(remote_permissions);
  871. if (remote_perms != MemoryPermission::Read && remote_perms != MemoryPermission::ReadWrite &&
  872. remote_perms != MemoryPermission::DontCare) {
  873. return ERR_INVALID_MEMORY_PERMISSIONS;
  874. }
  875. auto& kernel = Core::System::GetInstance().Kernel();
  876. auto& handle_table = kernel.HandleTable();
  877. auto shared_mem_handle =
  878. SharedMemory::Create(kernel, handle_table.Get<Process>(KernelHandle::CurrentProcess), size,
  879. local_perms, remote_perms);
  880. CASCADE_RESULT(*handle, handle_table.Create(shared_mem_handle));
  881. return RESULT_SUCCESS;
  882. }
  883. static ResultCode ClearEvent(Handle handle) {
  884. LOG_TRACE(Kernel_SVC, "called, event=0x{:08X}", handle);
  885. auto& kernel = Core::System::GetInstance().Kernel();
  886. SharedPtr<Event> evt = kernel.HandleTable().Get<Event>(handle);
  887. if (evt == nullptr)
  888. return ERR_INVALID_HANDLE;
  889. evt->Clear();
  890. return RESULT_SUCCESS;
  891. }
  892. namespace {
  893. struct FunctionDef {
  894. using Func = void();
  895. u32 id;
  896. Func* func;
  897. const char* name;
  898. };
  899. } // namespace
  900. static const FunctionDef SVC_Table[] = {
  901. {0x00, nullptr, "Unknown"},
  902. {0x01, SvcWrap<SetHeapSize>, "SetHeapSize"},
  903. {0x02, nullptr, "SetMemoryPermission"},
  904. {0x03, SvcWrap<SetMemoryAttribute>, "SetMemoryAttribute"},
  905. {0x04, SvcWrap<MapMemory>, "MapMemory"},
  906. {0x05, SvcWrap<UnmapMemory>, "UnmapMemory"},
  907. {0x06, SvcWrap<QueryMemory>, "QueryMemory"},
  908. {0x07, SvcWrap<ExitProcess>, "ExitProcess"},
  909. {0x08, SvcWrap<CreateThread>, "CreateThread"},
  910. {0x09, SvcWrap<StartThread>, "StartThread"},
  911. {0x0A, SvcWrap<ExitThread>, "ExitThread"},
  912. {0x0B, SvcWrap<SleepThread>, "SleepThread"},
  913. {0x0C, SvcWrap<GetThreadPriority>, "GetThreadPriority"},
  914. {0x0D, SvcWrap<SetThreadPriority>, "SetThreadPriority"},
  915. {0x0E, SvcWrap<GetThreadCoreMask>, "GetThreadCoreMask"},
  916. {0x0F, SvcWrap<SetThreadCoreMask>, "SetThreadCoreMask"},
  917. {0x10, SvcWrap<GetCurrentProcessorNumber>, "GetCurrentProcessorNumber"},
  918. {0x11, nullptr, "SignalEvent"},
  919. {0x12, SvcWrap<ClearEvent>, "ClearEvent"},
  920. {0x13, SvcWrap<MapSharedMemory>, "MapSharedMemory"},
  921. {0x14, SvcWrap<UnmapSharedMemory>, "UnmapSharedMemory"},
  922. {0x15, SvcWrap<CreateTransferMemory>, "CreateTransferMemory"},
  923. {0x16, SvcWrap<CloseHandle>, "CloseHandle"},
  924. {0x17, SvcWrap<ResetSignal>, "ResetSignal"},
  925. {0x18, SvcWrap<WaitSynchronization>, "WaitSynchronization"},
  926. {0x19, SvcWrap<CancelSynchronization>, "CancelSynchronization"},
  927. {0x1A, SvcWrap<ArbitrateLock>, "ArbitrateLock"},
  928. {0x1B, SvcWrap<ArbitrateUnlock>, "ArbitrateUnlock"},
  929. {0x1C, SvcWrap<WaitProcessWideKeyAtomic>, "WaitProcessWideKeyAtomic"},
  930. {0x1D, SvcWrap<SignalProcessWideKey>, "SignalProcessWideKey"},
  931. {0x1E, SvcWrap<GetSystemTick>, "GetSystemTick"},
  932. {0x1F, SvcWrap<ConnectToNamedPort>, "ConnectToNamedPort"},
  933. {0x20, nullptr, "SendSyncRequestLight"},
  934. {0x21, SvcWrap<SendSyncRequest>, "SendSyncRequest"},
  935. {0x22, nullptr, "SendSyncRequestWithUserBuffer"},
  936. {0x23, nullptr, "SendAsyncRequestWithUserBuffer"},
  937. {0x24, SvcWrap<GetProcessId>, "GetProcessId"},
  938. {0x25, SvcWrap<GetThreadId>, "GetThreadId"},
  939. {0x26, SvcWrap<Break>, "Break"},
  940. {0x27, SvcWrap<OutputDebugString>, "OutputDebugString"},
  941. {0x28, nullptr, "ReturnFromException"},
  942. {0x29, SvcWrap<GetInfo>, "GetInfo"},
  943. {0x2A, nullptr, "FlushEntireDataCache"},
  944. {0x2B, nullptr, "FlushDataCache"},
  945. {0x2C, nullptr, "MapPhysicalMemory"},
  946. {0x2D, nullptr, "UnmapPhysicalMemory"},
  947. {0x2E, nullptr, "GetFutureThreadInfo"},
  948. {0x2F, nullptr, "GetLastThreadInfo"},
  949. {0x30, nullptr, "GetResourceLimitLimitValue"},
  950. {0x31, nullptr, "GetResourceLimitCurrentValue"},
  951. {0x32, SvcWrap<SetThreadActivity>, "SetThreadActivity"},
  952. {0x33, SvcWrap<GetThreadContext>, "GetThreadContext"},
  953. {0x34, SvcWrap<WaitForAddress>, "WaitForAddress"},
  954. {0x35, SvcWrap<SignalToAddress>, "SignalToAddress"},
  955. {0x36, nullptr, "Unknown"},
  956. {0x37, nullptr, "Unknown"},
  957. {0x38, nullptr, "Unknown"},
  958. {0x39, nullptr, "Unknown"},
  959. {0x3A, nullptr, "Unknown"},
  960. {0x3B, nullptr, "Unknown"},
  961. {0x3C, nullptr, "DumpInfo"},
  962. {0x3D, nullptr, "DumpInfoNew"},
  963. {0x3E, nullptr, "Unknown"},
  964. {0x3F, nullptr, "Unknown"},
  965. {0x40, nullptr, "CreateSession"},
  966. {0x41, nullptr, "AcceptSession"},
  967. {0x42, nullptr, "ReplyAndReceiveLight"},
  968. {0x43, nullptr, "ReplyAndReceive"},
  969. {0x44, nullptr, "ReplyAndReceiveWithUserBuffer"},
  970. {0x45, nullptr, "CreateEvent"},
  971. {0x46, nullptr, "Unknown"},
  972. {0x47, nullptr, "Unknown"},
  973. {0x48, nullptr, "MapPhysicalMemoryUnsafe"},
  974. {0x49, nullptr, "UnmapPhysicalMemoryUnsafe"},
  975. {0x4A, nullptr, "SetUnsafeLimit"},
  976. {0x4B, nullptr, "CreateCodeMemory"},
  977. {0x4C, nullptr, "ControlCodeMemory"},
  978. {0x4D, nullptr, "SleepSystem"},
  979. {0x4E, nullptr, "ReadWriteRegister"},
  980. {0x4F, nullptr, "SetProcessActivity"},
  981. {0x50, SvcWrap<CreateSharedMemory>, "CreateSharedMemory"},
  982. {0x51, nullptr, "MapTransferMemory"},
  983. {0x52, nullptr, "UnmapTransferMemory"},
  984. {0x53, nullptr, "CreateInterruptEvent"},
  985. {0x54, nullptr, "QueryPhysicalAddress"},
  986. {0x55, nullptr, "QueryIoMapping"},
  987. {0x56, nullptr, "CreateDeviceAddressSpace"},
  988. {0x57, nullptr, "AttachDeviceAddressSpace"},
  989. {0x58, nullptr, "DetachDeviceAddressSpace"},
  990. {0x59, nullptr, "MapDeviceAddressSpaceByForce"},
  991. {0x5A, nullptr, "MapDeviceAddressSpaceAligned"},
  992. {0x5B, nullptr, "MapDeviceAddressSpace"},
  993. {0x5C, nullptr, "UnmapDeviceAddressSpace"},
  994. {0x5D, nullptr, "InvalidateProcessDataCache"},
  995. {0x5E, nullptr, "StoreProcessDataCache"},
  996. {0x5F, nullptr, "FlushProcessDataCache"},
  997. {0x60, nullptr, "DebugActiveProcess"},
  998. {0x61, nullptr, "BreakDebugProcess"},
  999. {0x62, nullptr, "TerminateDebugProcess"},
  1000. {0x63, nullptr, "GetDebugEvent"},
  1001. {0x64, nullptr, "ContinueDebugEvent"},
  1002. {0x65, nullptr, "GetProcessList"},
  1003. {0x66, nullptr, "GetThreadList"},
  1004. {0x67, nullptr, "GetDebugThreadContext"},
  1005. {0x68, nullptr, "SetDebugThreadContext"},
  1006. {0x69, nullptr, "QueryDebugProcessMemory"},
  1007. {0x6A, nullptr, "ReadDebugProcessMemory"},
  1008. {0x6B, nullptr, "WriteDebugProcessMemory"},
  1009. {0x6C, nullptr, "SetHardwareBreakPoint"},
  1010. {0x6D, nullptr, "GetDebugThreadParam"},
  1011. {0x6E, nullptr, "Unknown"},
  1012. {0x6F, nullptr, "GetSystemInfo"},
  1013. {0x70, nullptr, "CreatePort"},
  1014. {0x71, nullptr, "ManageNamedPort"},
  1015. {0x72, nullptr, "ConnectToPort"},
  1016. {0x73, nullptr, "SetProcessMemoryPermission"},
  1017. {0x74, nullptr, "MapProcessMemory"},
  1018. {0x75, nullptr, "UnmapProcessMemory"},
  1019. {0x76, nullptr, "QueryProcessMemory"},
  1020. {0x77, nullptr, "MapProcessCodeMemory"},
  1021. {0x78, nullptr, "UnmapProcessCodeMemory"},
  1022. {0x79, nullptr, "CreateProcess"},
  1023. {0x7A, nullptr, "StartProcess"},
  1024. {0x7B, nullptr, "TerminateProcess"},
  1025. {0x7C, nullptr, "GetProcessInfo"},
  1026. {0x7D, nullptr, "CreateResourceLimit"},
  1027. {0x7E, nullptr, "SetResourceLimitLimitValue"},
  1028. {0x7F, nullptr, "CallSecureMonitor"},
  1029. };
  1030. static const FunctionDef* GetSVCInfo(u32 func_num) {
  1031. if (func_num >= std::size(SVC_Table)) {
  1032. LOG_ERROR(Kernel_SVC, "Unknown svc=0x{:02X}", func_num);
  1033. return nullptr;
  1034. }
  1035. return &SVC_Table[func_num];
  1036. }
  1037. MICROPROFILE_DEFINE(Kernel_SVC, "Kernel", "SVC", MP_RGB(70, 200, 70));
  1038. void CallSVC(u32 immediate) {
  1039. MICROPROFILE_SCOPE(Kernel_SVC);
  1040. // Lock the global kernel mutex when we enter the kernel HLE.
  1041. std::lock_guard<std::recursive_mutex> lock(HLE::g_hle_lock);
  1042. const FunctionDef* info = GetSVCInfo(immediate);
  1043. if (info) {
  1044. if (info->func) {
  1045. info->func();
  1046. } else {
  1047. LOG_CRITICAL(Kernel_SVC, "Unimplemented SVC function {}(..)", info->name);
  1048. }
  1049. } else {
  1050. LOG_CRITICAL(Kernel_SVC, "Unknown SVC function 0x{:X}", immediate);
  1051. }
  1052. }
  1053. } // namespace Kernel