svc.cpp 48 KB

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