svc.cpp 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438
  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_manager.h"
  17. #include "core/core_timing.h"
  18. #include "core/core_timing_util.h"
  19. #include "core/hle/kernel/address_arbiter.h"
  20. #include "core/hle/kernel/client_port.h"
  21. #include "core/hle/kernel/client_session.h"
  22. #include "core/hle/kernel/errors.h"
  23. #include "core/hle/kernel/handle_table.h"
  24. #include "core/hle/kernel/kernel.h"
  25. #include "core/hle/kernel/memory/memory_block.h"
  26. #include "core/hle/kernel/memory/page_table.h"
  27. #include "core/hle/kernel/mutex.h"
  28. #include "core/hle/kernel/physical_core.h"
  29. #include "core/hle/kernel/process.h"
  30. #include "core/hle/kernel/readable_event.h"
  31. #include "core/hle/kernel/resource_limit.h"
  32. #include "core/hle/kernel/scheduler.h"
  33. #include "core/hle/kernel/shared_memory.h"
  34. #include "core/hle/kernel/svc.h"
  35. #include "core/hle/kernel/svc_types.h"
  36. #include "core/hle/kernel/svc_wrap.h"
  37. #include "core/hle/kernel/synchronization.h"
  38. #include "core/hle/kernel/thread.h"
  39. #include "core/hle/kernel/time_manager.h"
  40. #include "core/hle/kernel/transfer_memory.h"
  41. #include "core/hle/kernel/writable_event.h"
  42. #include "core/hle/lock.h"
  43. #include "core/hle/result.h"
  44. #include "core/hle/service/service.h"
  45. #include "core/memory.h"
  46. #include "core/reporter.h"
  47. namespace Kernel::Svc {
  48. namespace {
  49. // Checks if address + size is greater than the given address
  50. // This can return false if the size causes an overflow of a 64-bit type
  51. // or if the given size is zero.
  52. constexpr bool IsValidAddressRange(VAddr address, u64 size) {
  53. return address + size > address;
  54. }
  55. // Helper function that performs the common sanity checks for svcMapMemory
  56. // and svcUnmapMemory. This is doable, as both functions perform their sanitizing
  57. // in the same order.
  58. ResultCode MapUnmapMemorySanityChecks(const Memory::PageTable& manager, VAddr dst_addr,
  59. VAddr src_addr, u64 size) {
  60. if (!Common::Is4KBAligned(dst_addr)) {
  61. LOG_ERROR(Kernel_SVC, "Destination address is not aligned to 4KB, 0x{:016X}", dst_addr);
  62. return ERR_INVALID_ADDRESS;
  63. }
  64. if (!Common::Is4KBAligned(src_addr)) {
  65. LOG_ERROR(Kernel_SVC, "Source address is not aligned to 4KB, 0x{:016X}", src_addr);
  66. return ERR_INVALID_SIZE;
  67. }
  68. if (size == 0) {
  69. LOG_ERROR(Kernel_SVC, "Size is 0");
  70. return ERR_INVALID_SIZE;
  71. }
  72. if (!Common::Is4KBAligned(size)) {
  73. LOG_ERROR(Kernel_SVC, "Size is not aligned to 4KB, 0x{:016X}", size);
  74. return ERR_INVALID_SIZE;
  75. }
  76. if (!IsValidAddressRange(dst_addr, size)) {
  77. LOG_ERROR(Kernel_SVC,
  78. "Destination is not a valid address range, addr=0x{:016X}, size=0x{:016X}",
  79. dst_addr, size);
  80. return ERR_INVALID_ADDRESS_STATE;
  81. }
  82. if (!IsValidAddressRange(src_addr, size)) {
  83. LOG_ERROR(Kernel_SVC, "Source is not a valid address range, addr=0x{:016X}, size=0x{:016X}",
  84. src_addr, size);
  85. return ERR_INVALID_ADDRESS_STATE;
  86. }
  87. if (!manager.IsInsideAddressSpace(src_addr, size)) {
  88. LOG_ERROR(Kernel_SVC,
  89. "Source is not within the address space, addr=0x{:016X}, size=0x{:016X}",
  90. src_addr, size);
  91. return ERR_INVALID_ADDRESS_STATE;
  92. }
  93. if (manager.IsOutsideStackRegion(dst_addr, size)) {
  94. LOG_ERROR(Kernel_SVC,
  95. "Destination is not within the stack region, addr=0x{:016X}, size=0x{:016X}",
  96. dst_addr, size);
  97. return ERR_INVALID_MEMORY_RANGE;
  98. }
  99. if (manager.IsInsideHeapRegion(dst_addr, size)) {
  100. LOG_ERROR(Kernel_SVC,
  101. "Destination does not fit within the heap region, addr=0x{:016X}, "
  102. "size=0x{:016X}",
  103. dst_addr, size);
  104. return ERR_INVALID_MEMORY_RANGE;
  105. }
  106. if (manager.IsInsideAliasRegion(dst_addr, size)) {
  107. LOG_ERROR(Kernel_SVC,
  108. "Destination does not fit within the map region, addr=0x{:016X}, "
  109. "size=0x{:016X}",
  110. dst_addr, size);
  111. return ERR_INVALID_MEMORY_RANGE;
  112. }
  113. return RESULT_SUCCESS;
  114. }
  115. enum class ResourceLimitValueType {
  116. CurrentValue,
  117. LimitValue,
  118. };
  119. ResultVal<s64> RetrieveResourceLimitValue(Core::System& system, Handle resource_limit,
  120. u32 resource_type, ResourceLimitValueType value_type) {
  121. const auto type = static_cast<ResourceType>(resource_type);
  122. if (!IsValidResourceType(type)) {
  123. LOG_ERROR(Kernel_SVC, "Invalid resource limit type: '{}'", resource_type);
  124. return ERR_INVALID_ENUM_VALUE;
  125. }
  126. const auto* const current_process = system.Kernel().CurrentProcess();
  127. ASSERT(current_process != nullptr);
  128. const auto resource_limit_object =
  129. current_process->GetHandleTable().Get<ResourceLimit>(resource_limit);
  130. if (!resource_limit_object) {
  131. LOG_ERROR(Kernel_SVC, "Handle to non-existent resource limit instance used. Handle={:08X}",
  132. resource_limit);
  133. return ERR_INVALID_HANDLE;
  134. }
  135. if (value_type == ResourceLimitValueType::CurrentValue) {
  136. return MakeResult(resource_limit_object->GetCurrentResourceValue(type));
  137. }
  138. return MakeResult(resource_limit_object->GetMaxResourceValue(type));
  139. }
  140. } // Anonymous namespace
  141. /// Set the process heap to a given Size. It can both extend and shrink the heap.
  142. static ResultCode SetHeapSize(Core::System& system, VAddr* heap_addr, u64 heap_size) {
  143. LOG_TRACE(Kernel_SVC, "called, heap_size=0x{:X}", heap_size);
  144. // Size must be a multiple of 0x200000 (2MB) and be equal to or less than 8GB.
  145. if ((heap_size % 0x200000) != 0) {
  146. LOG_ERROR(Kernel_SVC, "The heap size is not a multiple of 2MB, heap_size=0x{:016X}",
  147. heap_size);
  148. return ERR_INVALID_SIZE;
  149. }
  150. if (heap_size >= 0x200000000) {
  151. LOG_ERROR(Kernel_SVC, "The heap size is not less than 8GB, heap_size=0x{:016X}", heap_size);
  152. return ERR_INVALID_SIZE;
  153. }
  154. auto& page_table{system.Kernel().CurrentProcess()->PageTable()};
  155. CASCADE_RESULT(*heap_addr, page_table.SetHeapSize(heap_size));
  156. return RESULT_SUCCESS;
  157. }
  158. static ResultCode SetHeapSize32(Core::System& system, u32* heap_addr, u32 heap_size) {
  159. VAddr temp_heap_addr{};
  160. const ResultCode result{SetHeapSize(system, &temp_heap_addr, heap_size)};
  161. *heap_addr = static_cast<u32>(temp_heap_addr);
  162. return result;
  163. }
  164. static ResultCode SetMemoryAttribute(Core::System& system, VAddr address, u64 size, u32 mask,
  165. u32 attribute) {
  166. LOG_DEBUG(Kernel_SVC,
  167. "called, address=0x{:016X}, size=0x{:X}, mask=0x{:08X}, attribute=0x{:08X}", address,
  168. size, mask, attribute);
  169. if (!Common::Is4KBAligned(address)) {
  170. LOG_ERROR(Kernel_SVC, "Address not page aligned (0x{:016X})", address);
  171. return ERR_INVALID_ADDRESS;
  172. }
  173. if (size == 0 || !Common::Is4KBAligned(size)) {
  174. LOG_ERROR(Kernel_SVC, "Invalid size (0x{:X}). Size must be non-zero and page aligned.",
  175. size);
  176. return ERR_INVALID_ADDRESS;
  177. }
  178. if (!IsValidAddressRange(address, size)) {
  179. LOG_ERROR(Kernel_SVC, "Address range overflowed (Address: 0x{:016X}, Size: 0x{:016X})",
  180. address, size);
  181. return ERR_INVALID_ADDRESS_STATE;
  182. }
  183. const auto attributes{static_cast<Memory::MemoryAttribute>(mask | attribute)};
  184. if (attributes != static_cast<Memory::MemoryAttribute>(mask) ||
  185. (attributes | Memory::MemoryAttribute::Uncached) != Memory::MemoryAttribute::Uncached) {
  186. LOG_ERROR(Kernel_SVC,
  187. "Memory attribute doesn't match the given mask (Attribute: 0x{:X}, Mask: {:X}",
  188. attribute, mask);
  189. return ERR_INVALID_COMBINATION;
  190. }
  191. auto& page_table{system.Kernel().CurrentProcess()->PageTable()};
  192. return page_table.SetMemoryAttribute(address, size, static_cast<Memory::MemoryAttribute>(mask),
  193. static_cast<Memory::MemoryAttribute>(attribute));
  194. }
  195. /// Maps a memory range into a different range.
  196. static ResultCode MapMemory(Core::System& system, VAddr dst_addr, VAddr src_addr, u64 size) {
  197. LOG_TRACE(Kernel_SVC, "called, dst_addr=0x{:X}, src_addr=0x{:X}, size=0x{:X}", dst_addr,
  198. src_addr, size);
  199. auto& page_table{system.Kernel().CurrentProcess()->PageTable()};
  200. if (const ResultCode result{MapUnmapMemorySanityChecks(page_table, dst_addr, src_addr, size)};
  201. result.IsError()) {
  202. return result;
  203. }
  204. return page_table.Map(dst_addr, src_addr, size);
  205. }
  206. /// Unmaps a region that was previously mapped with svcMapMemory
  207. static ResultCode UnmapMemory(Core::System& system, VAddr dst_addr, VAddr src_addr, u64 size) {
  208. LOG_TRACE(Kernel_SVC, "called, dst_addr=0x{:X}, src_addr=0x{:X}, size=0x{:X}", dst_addr,
  209. src_addr, size);
  210. auto& page_table{system.Kernel().CurrentProcess()->PageTable()};
  211. if (const ResultCode result{MapUnmapMemorySanityChecks(page_table, dst_addr, src_addr, size)};
  212. result.IsError()) {
  213. return result;
  214. }
  215. return page_table.Unmap(dst_addr, src_addr, size);
  216. }
  217. /// Connect to an OS service given the port name, returns the handle to the port to out
  218. static ResultCode ConnectToNamedPort(Core::System& system, Handle* out_handle,
  219. VAddr port_name_address) {
  220. auto& memory = system.Memory();
  221. if (!memory.IsValidVirtualAddress(port_name_address)) {
  222. LOG_ERROR(Kernel_SVC,
  223. "Port Name Address is not a valid virtual address, port_name_address=0x{:016X}",
  224. port_name_address);
  225. return ERR_NOT_FOUND;
  226. }
  227. static constexpr std::size_t PortNameMaxLength = 11;
  228. // Read 1 char beyond the max allowed port name to detect names that are too long.
  229. const std::string port_name = memory.ReadCString(port_name_address, PortNameMaxLength + 1);
  230. if (port_name.size() > PortNameMaxLength) {
  231. LOG_ERROR(Kernel_SVC, "Port name is too long, expected {} but got {}", PortNameMaxLength,
  232. port_name.size());
  233. return ERR_OUT_OF_RANGE;
  234. }
  235. LOG_TRACE(Kernel_SVC, "called port_name={}", port_name);
  236. auto& kernel = system.Kernel();
  237. const auto it = kernel.FindNamedPort(port_name);
  238. if (!kernel.IsValidNamedPort(it)) {
  239. LOG_WARNING(Kernel_SVC, "tried to connect to unknown port: {}", port_name);
  240. return ERR_NOT_FOUND;
  241. }
  242. ASSERT(kernel.CurrentProcess()->GetResourceLimit()->Reserve(ResourceType::Sessions, 1));
  243. auto client_port = it->second;
  244. std::shared_ptr<ClientSession> client_session;
  245. CASCADE_RESULT(client_session, client_port->Connect());
  246. // Return the client session
  247. auto& handle_table = kernel.CurrentProcess()->GetHandleTable();
  248. CASCADE_RESULT(*out_handle, handle_table.Create(client_session));
  249. return RESULT_SUCCESS;
  250. }
  251. static ResultCode ConnectToNamedPort32(Core::System& system, Handle* out_handle,
  252. u32 port_name_address) {
  253. return ConnectToNamedPort(system, out_handle, port_name_address);
  254. }
  255. /// Makes a blocking IPC call to an OS service.
  256. static ResultCode SendSyncRequest(Core::System& system, Handle handle) {
  257. const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
  258. std::shared_ptr<ClientSession> session = handle_table.Get<ClientSession>(handle);
  259. if (!session) {
  260. LOG_ERROR(Kernel_SVC, "called with invalid handle=0x{:08X}", handle);
  261. return ERR_INVALID_HANDLE;
  262. }
  263. LOG_TRACE(Kernel_SVC, "called handle=0x{:08X}({})", handle, session->GetName());
  264. auto thread = system.CurrentScheduler().GetCurrentThread();
  265. {
  266. SchedulerLock lock(system.Kernel());
  267. thread->InvalidateHLECallback();
  268. thread->SetStatus(ThreadStatus::WaitIPC);
  269. session->SendSyncRequest(SharedFrom(thread), system.Memory());
  270. }
  271. ResultCode result = thread->GetSignalingResult();
  272. if (thread->HasHLECallback()) {
  273. Handle event_handle = thread->GetHLETimeEvent();
  274. if (event_handle != InvalidHandle) {
  275. auto& time_manager = system.Kernel().TimeManager();
  276. time_manager.UnscheduleTimeEvent(event_handle);
  277. }
  278. thread->InvokeHLECallback(ThreadWakeupReason::Timeout, SharedFrom(thread), nullptr, 0);
  279. }
  280. return result;
  281. }
  282. static ResultCode SendSyncRequest32(Core::System& system, Handle handle) {
  283. return SendSyncRequest(system, handle);
  284. }
  285. /// Get the ID for the specified thread.
  286. static ResultCode GetThreadId(Core::System& system, u64* thread_id, Handle thread_handle) {
  287. LOG_TRACE(Kernel_SVC, "called thread=0x{:08X}", thread_handle);
  288. const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
  289. const std::shared_ptr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  290. if (!thread) {
  291. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", thread_handle);
  292. return ERR_INVALID_HANDLE;
  293. }
  294. *thread_id = thread->GetThreadID();
  295. return RESULT_SUCCESS;
  296. }
  297. static ResultCode GetThreadId32(Core::System& system, u32* thread_id_low, u32* thread_id_high,
  298. Handle thread_handle) {
  299. u64 thread_id{};
  300. const ResultCode result{GetThreadId(system, &thread_id, thread_handle)};
  301. *thread_id_low = static_cast<u32>(thread_id >> 32);
  302. *thread_id_high = static_cast<u32>(thread_id & std::numeric_limits<u32>::max());
  303. return result;
  304. }
  305. /// Gets the ID of the specified process or a specified thread's owning process.
  306. static ResultCode GetProcessId(Core::System& system, u64* process_id, Handle handle) {
  307. LOG_DEBUG(Kernel_SVC, "called handle=0x{:08X}", handle);
  308. const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
  309. const std::shared_ptr<Process> process = handle_table.Get<Process>(handle);
  310. if (process) {
  311. *process_id = process->GetProcessID();
  312. return RESULT_SUCCESS;
  313. }
  314. const std::shared_ptr<Thread> thread = handle_table.Get<Thread>(handle);
  315. if (thread) {
  316. const Process* const owner_process = thread->GetOwnerProcess();
  317. if (!owner_process) {
  318. LOG_ERROR(Kernel_SVC, "Non-existent owning process encountered.");
  319. return ERR_INVALID_HANDLE;
  320. }
  321. *process_id = owner_process->GetProcessID();
  322. return RESULT_SUCCESS;
  323. }
  324. // NOTE: This should also handle debug objects before returning.
  325. LOG_ERROR(Kernel_SVC, "Handle does not exist, handle=0x{:08X}", handle);
  326. return ERR_INVALID_HANDLE;
  327. }
  328. /// Wait for the given handles to synchronize, timeout after the specified nanoseconds
  329. static ResultCode WaitSynchronization(Core::System& system, Handle* index, VAddr handles_address,
  330. u64 handle_count, s64 nano_seconds) {
  331. LOG_TRACE(Kernel_SVC, "called handles_address=0x{:X}, handle_count={}, nano_seconds={}",
  332. handles_address, handle_count, nano_seconds);
  333. auto& memory = system.Memory();
  334. if (!memory.IsValidVirtualAddress(handles_address)) {
  335. LOG_ERROR(Kernel_SVC,
  336. "Handle address is not a valid virtual address, handle_address=0x{:016X}",
  337. handles_address);
  338. return ERR_INVALID_POINTER;
  339. }
  340. static constexpr u64 MaxHandles = 0x40;
  341. if (handle_count > MaxHandles) {
  342. LOG_ERROR(Kernel_SVC, "Handle count specified is too large, expected {} but got {}",
  343. MaxHandles, handle_count);
  344. return ERR_OUT_OF_RANGE;
  345. }
  346. auto* const thread = system.CurrentScheduler().GetCurrentThread();
  347. auto& kernel = system.Kernel();
  348. using ObjectPtr = Thread::ThreadSynchronizationObjects::value_type;
  349. Thread::ThreadSynchronizationObjects objects(handle_count);
  350. const auto& handle_table = kernel.CurrentProcess()->GetHandleTable();
  351. for (u64 i = 0; i < handle_count; ++i) {
  352. const Handle handle = memory.Read32(handles_address + i * sizeof(Handle));
  353. const auto object = handle_table.Get<SynchronizationObject>(handle);
  354. if (object == nullptr) {
  355. LOG_ERROR(Kernel_SVC, "Object is a nullptr");
  356. return ERR_INVALID_HANDLE;
  357. }
  358. objects[i] = object;
  359. }
  360. auto& synchronization = kernel.Synchronization();
  361. const auto [result, handle_result] = synchronization.WaitFor(objects, nano_seconds);
  362. *index = handle_result;
  363. return result;
  364. }
  365. static ResultCode WaitSynchronization32(Core::System& system, u32 timeout_low, u32 handles_address,
  366. s32 handle_count, u32 timeout_high, Handle* index) {
  367. const s64 nano_seconds{(static_cast<s64>(timeout_high) << 32) | static_cast<s64>(timeout_low)};
  368. return WaitSynchronization(system, index, handles_address, handle_count, nano_seconds);
  369. }
  370. /// Resumes a thread waiting on WaitSynchronization
  371. static ResultCode CancelSynchronization(Core::System& system, Handle thread_handle) {
  372. LOG_TRACE(Kernel_SVC, "called thread=0x{:X}", thread_handle);
  373. const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
  374. std::shared_ptr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  375. if (!thread) {
  376. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, thread_handle=0x{:08X}",
  377. thread_handle);
  378. return ERR_INVALID_HANDLE;
  379. }
  380. thread->CancelWait();
  381. return RESULT_SUCCESS;
  382. }
  383. /// Attempts to locks a mutex, creating it if it does not already exist
  384. static ResultCode ArbitrateLock(Core::System& system, Handle holding_thread_handle,
  385. VAddr mutex_addr, Handle requesting_thread_handle) {
  386. LOG_TRACE(Kernel_SVC,
  387. "called holding_thread_handle=0x{:08X}, mutex_addr=0x{:X}, "
  388. "requesting_current_thread_handle=0x{:08X}",
  389. holding_thread_handle, mutex_addr, requesting_thread_handle);
  390. if (Core::Memory::IsKernelVirtualAddress(mutex_addr)) {
  391. LOG_ERROR(Kernel_SVC, "Mutex Address is a kernel virtual address, mutex_addr={:016X}",
  392. mutex_addr);
  393. return ERR_INVALID_ADDRESS_STATE;
  394. }
  395. if (!Common::IsWordAligned(mutex_addr)) {
  396. LOG_ERROR(Kernel_SVC, "Mutex Address is not word aligned, mutex_addr={:016X}", mutex_addr);
  397. return ERR_INVALID_ADDRESS;
  398. }
  399. auto* const current_process = system.Kernel().CurrentProcess();
  400. return current_process->GetMutex().TryAcquire(mutex_addr, holding_thread_handle,
  401. requesting_thread_handle);
  402. }
  403. /// Unlock a mutex
  404. static ResultCode ArbitrateUnlock(Core::System& system, VAddr mutex_addr) {
  405. LOG_TRACE(Kernel_SVC, "called mutex_addr=0x{:X}", mutex_addr);
  406. if (Core::Memory::IsKernelVirtualAddress(mutex_addr)) {
  407. LOG_ERROR(Kernel_SVC, "Mutex Address is a kernel virtual address, mutex_addr={:016X}",
  408. mutex_addr);
  409. return ERR_INVALID_ADDRESS_STATE;
  410. }
  411. if (!Common::IsWordAligned(mutex_addr)) {
  412. LOG_ERROR(Kernel_SVC, "Mutex Address is not word aligned, mutex_addr={:016X}", mutex_addr);
  413. return ERR_INVALID_ADDRESS;
  414. }
  415. auto* const current_process = system.Kernel().CurrentProcess();
  416. return current_process->GetMutex().Release(mutex_addr);
  417. }
  418. enum class BreakType : u32 {
  419. Panic = 0,
  420. AssertionFailed = 1,
  421. PreNROLoad = 3,
  422. PostNROLoad = 4,
  423. PreNROUnload = 5,
  424. PostNROUnload = 6,
  425. CppException = 7,
  426. };
  427. struct BreakReason {
  428. union {
  429. u32 raw;
  430. BitField<0, 30, BreakType> break_type;
  431. BitField<31, 1, u32> signal_debugger;
  432. };
  433. };
  434. /// Break program execution
  435. static void Break(Core::System& system, u32 reason, u64 info1, u64 info2) {
  436. BreakReason break_reason{reason};
  437. bool has_dumped_buffer{};
  438. std::vector<u8> debug_buffer;
  439. const auto handle_debug_buffer = [&](VAddr addr, u64 sz) {
  440. if (sz == 0 || addr == 0 || has_dumped_buffer) {
  441. return;
  442. }
  443. auto& memory = system.Memory();
  444. // This typically is an error code so we're going to assume this is the case
  445. if (sz == sizeof(u32)) {
  446. LOG_CRITICAL(Debug_Emulated, "debug_buffer_err_code={:X}", memory.Read32(addr));
  447. } else {
  448. // We don't know what's in here so we'll hexdump it
  449. debug_buffer.resize(sz);
  450. memory.ReadBlock(addr, debug_buffer.data(), sz);
  451. std::string hexdump;
  452. for (std::size_t i = 0; i < debug_buffer.size(); i++) {
  453. hexdump += fmt::format("{:02X} ", debug_buffer[i]);
  454. if (i != 0 && i % 16 == 0) {
  455. hexdump += '\n';
  456. }
  457. }
  458. LOG_CRITICAL(Debug_Emulated, "debug_buffer=\n{}", hexdump);
  459. }
  460. has_dumped_buffer = true;
  461. };
  462. switch (break_reason.break_type) {
  463. case BreakType::Panic:
  464. LOG_CRITICAL(Debug_Emulated, "Signalling debugger, PANIC! info1=0x{:016X}, info2=0x{:016X}",
  465. info1, info2);
  466. handle_debug_buffer(info1, info2);
  467. break;
  468. case BreakType::AssertionFailed:
  469. LOG_CRITICAL(Debug_Emulated,
  470. "Signalling debugger, Assertion failed! info1=0x{:016X}, info2=0x{:016X}",
  471. info1, info2);
  472. handle_debug_buffer(info1, info2);
  473. break;
  474. case BreakType::PreNROLoad:
  475. LOG_WARNING(
  476. Debug_Emulated,
  477. "Signalling debugger, Attempting to load an NRO at 0x{:016X} with size 0x{:016X}",
  478. info1, info2);
  479. break;
  480. case BreakType::PostNROLoad:
  481. LOG_WARNING(Debug_Emulated,
  482. "Signalling debugger, Loaded an NRO at 0x{:016X} with size 0x{:016X}", info1,
  483. info2);
  484. break;
  485. case BreakType::PreNROUnload:
  486. LOG_WARNING(
  487. Debug_Emulated,
  488. "Signalling debugger, Attempting to unload an NRO at 0x{:016X} with size 0x{:016X}",
  489. info1, info2);
  490. break;
  491. case BreakType::PostNROUnload:
  492. LOG_WARNING(Debug_Emulated,
  493. "Signalling debugger, Unloaded an NRO at 0x{:016X} with size 0x{:016X}", info1,
  494. info2);
  495. break;
  496. case BreakType::CppException:
  497. LOG_CRITICAL(Debug_Emulated, "Signalling debugger. Uncaught C++ exception encountered.");
  498. break;
  499. default:
  500. LOG_WARNING(
  501. Debug_Emulated,
  502. "Signalling debugger, Unknown break reason {}, info1=0x{:016X}, info2=0x{:016X}",
  503. static_cast<u32>(break_reason.break_type.Value()), info1, info2);
  504. handle_debug_buffer(info1, info2);
  505. break;
  506. }
  507. system.GetReporter().SaveSvcBreakReport(
  508. static_cast<u32>(break_reason.break_type.Value()), break_reason.signal_debugger, info1,
  509. info2, has_dumped_buffer ? std::make_optional(debug_buffer) : std::nullopt);
  510. if (!break_reason.signal_debugger) {
  511. LOG_CRITICAL(
  512. Debug_Emulated,
  513. "Emulated program broke execution! reason=0x{:016X}, info1=0x{:016X}, info2=0x{:016X}",
  514. reason, info1, info2);
  515. handle_debug_buffer(info1, info2);
  516. auto* const current_thread = system.CurrentScheduler().GetCurrentThread();
  517. const auto thread_processor_id = current_thread->GetProcessorID();
  518. system.ArmInterface(static_cast<std::size_t>(thread_processor_id)).LogBacktrace();
  519. system.Kernel().CurrentProcess()->PrepareForTermination();
  520. // Kill the current thread
  521. current_thread->Stop();
  522. system.PrepareReschedule();
  523. }
  524. }
  525. /// Used to output a message on a debug hardware unit - does nothing on a retail unit
  526. static void OutputDebugString([[maybe_unused]] Core::System& system, VAddr address, u64 len) {
  527. if (len == 0) {
  528. return;
  529. }
  530. std::string str(len, '\0');
  531. system.Memory().ReadBlock(address, str.data(), str.size());
  532. LOG_DEBUG(Debug_Emulated, "{}", str);
  533. }
  534. /// Gets system/memory information for the current process
  535. static ResultCode GetInfo(Core::System& system, u64* result, u64 info_id, u64 handle,
  536. u64 info_sub_id) {
  537. LOG_TRACE(Kernel_SVC, "called info_id=0x{:X}, info_sub_id=0x{:X}, handle=0x{:08X}", info_id,
  538. info_sub_id, handle);
  539. enum class GetInfoType : u64 {
  540. // 1.0.0+
  541. AllowedCPUCoreMask = 0,
  542. AllowedThreadPriorityMask = 1,
  543. MapRegionBaseAddr = 2,
  544. MapRegionSize = 3,
  545. HeapRegionBaseAddr = 4,
  546. HeapRegionSize = 5,
  547. TotalPhysicalMemoryAvailable = 6,
  548. TotalPhysicalMemoryUsed = 7,
  549. IsCurrentProcessBeingDebugged = 8,
  550. RegisterResourceLimit = 9,
  551. IdleTickCount = 10,
  552. RandomEntropy = 11,
  553. ThreadTickCount = 0xF0000002,
  554. // 2.0.0+
  555. ASLRRegionBaseAddr = 12,
  556. ASLRRegionSize = 13,
  557. StackRegionBaseAddr = 14,
  558. StackRegionSize = 15,
  559. // 3.0.0+
  560. SystemResourceSize = 16,
  561. SystemResourceUsage = 17,
  562. TitleId = 18,
  563. // 4.0.0+
  564. PrivilegedProcessId = 19,
  565. // 5.0.0+
  566. UserExceptionContextAddr = 20,
  567. // 6.0.0+
  568. TotalPhysicalMemoryAvailableWithoutSystemResource = 21,
  569. TotalPhysicalMemoryUsedWithoutSystemResource = 22,
  570. };
  571. const auto info_id_type = static_cast<GetInfoType>(info_id);
  572. switch (info_id_type) {
  573. case GetInfoType::AllowedCPUCoreMask:
  574. case GetInfoType::AllowedThreadPriorityMask:
  575. case GetInfoType::MapRegionBaseAddr:
  576. case GetInfoType::MapRegionSize:
  577. case GetInfoType::HeapRegionBaseAddr:
  578. case GetInfoType::HeapRegionSize:
  579. case GetInfoType::ASLRRegionBaseAddr:
  580. case GetInfoType::ASLRRegionSize:
  581. case GetInfoType::StackRegionBaseAddr:
  582. case GetInfoType::StackRegionSize:
  583. case GetInfoType::TotalPhysicalMemoryAvailable:
  584. case GetInfoType::TotalPhysicalMemoryUsed:
  585. case GetInfoType::SystemResourceSize:
  586. case GetInfoType::SystemResourceUsage:
  587. case GetInfoType::TitleId:
  588. case GetInfoType::UserExceptionContextAddr:
  589. case GetInfoType::TotalPhysicalMemoryAvailableWithoutSystemResource:
  590. case GetInfoType::TotalPhysicalMemoryUsedWithoutSystemResource: {
  591. if (info_sub_id != 0) {
  592. LOG_ERROR(Kernel_SVC, "Info sub id is non zero! info_id={}, info_sub_id={}", info_id,
  593. info_sub_id);
  594. return ERR_INVALID_ENUM_VALUE;
  595. }
  596. const auto& current_process_handle_table =
  597. system.Kernel().CurrentProcess()->GetHandleTable();
  598. const auto process = current_process_handle_table.Get<Process>(static_cast<Handle>(handle));
  599. if (!process) {
  600. LOG_ERROR(Kernel_SVC, "Process is not valid! info_id={}, info_sub_id={}, handle={:08X}",
  601. info_id, info_sub_id, handle);
  602. return ERR_INVALID_HANDLE;
  603. }
  604. switch (info_id_type) {
  605. case GetInfoType::AllowedCPUCoreMask:
  606. *result = process->GetCoreMask();
  607. return RESULT_SUCCESS;
  608. case GetInfoType::AllowedThreadPriorityMask:
  609. *result = process->GetPriorityMask();
  610. return RESULT_SUCCESS;
  611. case GetInfoType::MapRegionBaseAddr:
  612. *result = process->PageTable().GetAliasRegionStart();
  613. return RESULT_SUCCESS;
  614. case GetInfoType::MapRegionSize:
  615. *result = process->PageTable().GetAliasRegionSize();
  616. return RESULT_SUCCESS;
  617. case GetInfoType::HeapRegionBaseAddr:
  618. *result = process->PageTable().GetHeapRegionStart();
  619. return RESULT_SUCCESS;
  620. case GetInfoType::HeapRegionSize:
  621. *result = process->PageTable().GetHeapRegionSize();
  622. return RESULT_SUCCESS;
  623. case GetInfoType::ASLRRegionBaseAddr:
  624. *result = process->PageTable().GetAliasCodeRegionStart();
  625. return RESULT_SUCCESS;
  626. case GetInfoType::ASLRRegionSize:
  627. *result = process->PageTable().GetAliasCodeRegionSize();
  628. return RESULT_SUCCESS;
  629. case GetInfoType::StackRegionBaseAddr:
  630. *result = process->PageTable().GetStackRegionStart();
  631. return RESULT_SUCCESS;
  632. case GetInfoType::StackRegionSize:
  633. *result = process->PageTable().GetStackRegionSize();
  634. return RESULT_SUCCESS;
  635. case GetInfoType::TotalPhysicalMemoryAvailable:
  636. *result = process->GetTotalPhysicalMemoryAvailable();
  637. return RESULT_SUCCESS;
  638. case GetInfoType::TotalPhysicalMemoryUsed:
  639. *result = process->GetTotalPhysicalMemoryUsed();
  640. return RESULT_SUCCESS;
  641. case GetInfoType::SystemResourceSize:
  642. *result = process->GetSystemResourceSize();
  643. return RESULT_SUCCESS;
  644. case GetInfoType::SystemResourceUsage:
  645. LOG_WARNING(Kernel_SVC, "(STUBBED) Attempted to query system resource usage");
  646. *result = process->GetSystemResourceUsage();
  647. return RESULT_SUCCESS;
  648. case GetInfoType::TitleId:
  649. *result = process->GetTitleID();
  650. return RESULT_SUCCESS;
  651. case GetInfoType::UserExceptionContextAddr:
  652. *result = process->GetTLSRegionAddress();
  653. return RESULT_SUCCESS;
  654. case GetInfoType::TotalPhysicalMemoryAvailableWithoutSystemResource:
  655. *result = process->GetTotalPhysicalMemoryAvailableWithoutSystemResource();
  656. return RESULT_SUCCESS;
  657. case GetInfoType::TotalPhysicalMemoryUsedWithoutSystemResource:
  658. *result = process->GetTotalPhysicalMemoryUsedWithoutSystemResource();
  659. return RESULT_SUCCESS;
  660. default:
  661. break;
  662. }
  663. LOG_ERROR(Kernel_SVC, "Unimplemented svcGetInfo id=0x{:016X}", info_id);
  664. return ERR_INVALID_ENUM_VALUE;
  665. }
  666. case GetInfoType::IsCurrentProcessBeingDebugged:
  667. *result = 0;
  668. return RESULT_SUCCESS;
  669. case GetInfoType::RegisterResourceLimit: {
  670. if (handle != 0) {
  671. LOG_ERROR(Kernel, "Handle is non zero! handle={:08X}", handle);
  672. return ERR_INVALID_HANDLE;
  673. }
  674. if (info_sub_id != 0) {
  675. LOG_ERROR(Kernel, "Info sub id is non zero! info_id={}, info_sub_id={}", info_id,
  676. info_sub_id);
  677. return ERR_INVALID_COMBINATION;
  678. }
  679. Process* const current_process = system.Kernel().CurrentProcess();
  680. HandleTable& handle_table = current_process->GetHandleTable();
  681. const auto resource_limit = current_process->GetResourceLimit();
  682. if (!resource_limit) {
  683. *result = KernelHandle::InvalidHandle;
  684. // Yes, the kernel considers this a successful operation.
  685. return RESULT_SUCCESS;
  686. }
  687. const auto table_result = handle_table.Create(resource_limit);
  688. if (table_result.Failed()) {
  689. return table_result.Code();
  690. }
  691. *result = *table_result;
  692. return RESULT_SUCCESS;
  693. }
  694. case GetInfoType::RandomEntropy:
  695. if (handle != 0) {
  696. LOG_ERROR(Kernel_SVC, "Process Handle is non zero, expected 0 result but got {:016X}",
  697. handle);
  698. return ERR_INVALID_HANDLE;
  699. }
  700. if (info_sub_id >= Process::RANDOM_ENTROPY_SIZE) {
  701. LOG_ERROR(Kernel_SVC, "Entropy size is out of range, expected {} but got {}",
  702. Process::RANDOM_ENTROPY_SIZE, info_sub_id);
  703. return ERR_INVALID_COMBINATION;
  704. }
  705. *result = system.Kernel().CurrentProcess()->GetRandomEntropy(info_sub_id);
  706. return RESULT_SUCCESS;
  707. case GetInfoType::PrivilegedProcessId:
  708. LOG_WARNING(Kernel_SVC,
  709. "(STUBBED) Attempted to query privileged process id bounds, returned 0");
  710. *result = 0;
  711. return RESULT_SUCCESS;
  712. case GetInfoType::ThreadTickCount: {
  713. constexpr u64 num_cpus = 4;
  714. if (info_sub_id != 0xFFFFFFFFFFFFFFFF && info_sub_id >= num_cpus) {
  715. LOG_ERROR(Kernel_SVC, "Core count is out of range, expected {} but got {}", num_cpus,
  716. info_sub_id);
  717. return ERR_INVALID_COMBINATION;
  718. }
  719. const auto thread = system.Kernel().CurrentProcess()->GetHandleTable().Get<Thread>(
  720. static_cast<Handle>(handle));
  721. if (!thread) {
  722. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}",
  723. static_cast<Handle>(handle));
  724. return ERR_INVALID_HANDLE;
  725. }
  726. const auto& core_timing = system.CoreTiming();
  727. const auto& scheduler = system.CurrentScheduler();
  728. const auto* const current_thread = scheduler.GetCurrentThread();
  729. const bool same_thread = current_thread == thread.get();
  730. const u64 prev_ctx_ticks = scheduler.GetLastContextSwitchTicks();
  731. u64 out_ticks = 0;
  732. if (same_thread && info_sub_id == 0xFFFFFFFFFFFFFFFF) {
  733. const u64 thread_ticks = current_thread->GetTotalCPUTimeTicks();
  734. out_ticks = thread_ticks + (core_timing.GetCPUTicks() - prev_ctx_ticks);
  735. } else if (same_thread && info_sub_id == system.CurrentCoreIndex()) {
  736. out_ticks = core_timing.GetCPUTicks() - prev_ctx_ticks;
  737. }
  738. *result = out_ticks;
  739. return RESULT_SUCCESS;
  740. }
  741. default:
  742. LOG_ERROR(Kernel_SVC, "Unimplemented svcGetInfo id=0x{:016X}", info_id);
  743. return ERR_INVALID_ENUM_VALUE;
  744. }
  745. }
  746. static ResultCode GetInfo32(Core::System& system, u32* result_low, u32* result_high, u32 sub_id_low,
  747. u32 info_id, u32 handle, u32 sub_id_high) {
  748. const u64 sub_id{static_cast<u64>(sub_id_low | (static_cast<u64>(sub_id_high) << 32))};
  749. u64 res_value{};
  750. const ResultCode result{GetInfo(system, &res_value, info_id, handle, sub_id)};
  751. *result_high = static_cast<u32>(res_value >> 32);
  752. *result_low = static_cast<u32>(res_value & std::numeric_limits<u32>::max());
  753. return result;
  754. }
  755. /// Maps memory at a desired address
  756. static ResultCode MapPhysicalMemory(Core::System& system, VAddr addr, u64 size) {
  757. LOG_DEBUG(Kernel_SVC, "called, addr=0x{:016X}, size=0x{:X}", addr, size);
  758. if (!Common::Is4KBAligned(addr)) {
  759. LOG_ERROR(Kernel_SVC, "Address is not aligned to 4KB, 0x{:016X}", addr);
  760. return ERR_INVALID_ADDRESS;
  761. }
  762. if (!Common::Is4KBAligned(size)) {
  763. LOG_ERROR(Kernel_SVC, "Size is not aligned to 4KB, 0x{:X}", size);
  764. return ERR_INVALID_SIZE;
  765. }
  766. if (size == 0) {
  767. LOG_ERROR(Kernel_SVC, "Size is zero");
  768. return ERR_INVALID_SIZE;
  769. }
  770. if (!(addr < addr + size)) {
  771. LOG_ERROR(Kernel_SVC, "Size causes 64-bit overflow of address");
  772. return ERR_INVALID_MEMORY_RANGE;
  773. }
  774. Process* const current_process{system.Kernel().CurrentProcess()};
  775. auto& page_table{current_process->PageTable()};
  776. if (current_process->GetSystemResourceSize() == 0) {
  777. LOG_ERROR(Kernel_SVC, "System Resource Size is zero");
  778. return ERR_INVALID_STATE;
  779. }
  780. if (!page_table.IsInsideAddressSpace(addr, size)) {
  781. LOG_ERROR(Kernel_SVC,
  782. "Address is not within the address space, addr=0x{:016X}, size=0x{:016X}", addr,
  783. size);
  784. return ERR_INVALID_MEMORY_RANGE;
  785. }
  786. if (page_table.IsOutsideAliasRegion(addr, size)) {
  787. LOG_ERROR(Kernel_SVC,
  788. "Address is not within the alias region, addr=0x{:016X}, size=0x{:016X}", addr,
  789. size);
  790. return ERR_INVALID_MEMORY_RANGE;
  791. }
  792. return page_table.MapPhysicalMemory(addr, size);
  793. }
  794. /// Unmaps memory previously mapped via MapPhysicalMemory
  795. static ResultCode UnmapPhysicalMemory(Core::System& system, VAddr addr, u64 size) {
  796. LOG_DEBUG(Kernel_SVC, "called, addr=0x{:016X}, size=0x{:X}", addr, size);
  797. if (!Common::Is4KBAligned(addr)) {
  798. LOG_ERROR(Kernel_SVC, "Address is not aligned to 4KB, 0x{:016X}", addr);
  799. return ERR_INVALID_ADDRESS;
  800. }
  801. if (!Common::Is4KBAligned(size)) {
  802. LOG_ERROR(Kernel_SVC, "Size is not aligned to 4KB, 0x{:X}", size);
  803. return ERR_INVALID_SIZE;
  804. }
  805. if (size == 0) {
  806. LOG_ERROR(Kernel_SVC, "Size is zero");
  807. return ERR_INVALID_SIZE;
  808. }
  809. if (!(addr < addr + size)) {
  810. LOG_ERROR(Kernel_SVC, "Size causes 64-bit overflow of address");
  811. return ERR_INVALID_MEMORY_RANGE;
  812. }
  813. Process* const current_process{system.Kernel().CurrentProcess()};
  814. auto& page_table{current_process->PageTable()};
  815. if (current_process->GetSystemResourceSize() == 0) {
  816. LOG_ERROR(Kernel_SVC, "System Resource Size is zero");
  817. return ERR_INVALID_STATE;
  818. }
  819. if (!page_table.IsInsideAddressSpace(addr, size)) {
  820. LOG_ERROR(Kernel_SVC,
  821. "Address is not within the address space, addr=0x{:016X}, size=0x{:016X}", addr,
  822. size);
  823. return ERR_INVALID_MEMORY_RANGE;
  824. }
  825. if (page_table.IsOutsideAliasRegion(addr, size)) {
  826. LOG_ERROR(Kernel_SVC,
  827. "Address is not within the alias region, addr=0x{:016X}, size=0x{:016X}", addr,
  828. size);
  829. return ERR_INVALID_MEMORY_RANGE;
  830. }
  831. return page_table.UnmapPhysicalMemory(addr, size);
  832. }
  833. /// Sets the thread activity
  834. static ResultCode SetThreadActivity(Core::System& system, Handle handle, u32 activity) {
  835. LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, activity=0x{:08X}", handle, activity);
  836. if (activity > static_cast<u32>(ThreadActivity::Paused)) {
  837. return ERR_INVALID_ENUM_VALUE;
  838. }
  839. const auto* current_process = system.Kernel().CurrentProcess();
  840. const std::shared_ptr<Thread> thread = current_process->GetHandleTable().Get<Thread>(handle);
  841. if (!thread) {
  842. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", handle);
  843. return ERR_INVALID_HANDLE;
  844. }
  845. if (thread->GetOwnerProcess() != current_process) {
  846. LOG_ERROR(Kernel_SVC,
  847. "The current process does not own the current thread, thread_handle={:08X} "
  848. "thread_pid={}, "
  849. "current_process_pid={}",
  850. handle, thread->GetOwnerProcess()->GetProcessID(),
  851. current_process->GetProcessID());
  852. return ERR_INVALID_HANDLE;
  853. }
  854. if (thread.get() == system.CurrentScheduler().GetCurrentThread()) {
  855. LOG_ERROR(Kernel_SVC, "The thread handle specified is the current running thread");
  856. return ERR_BUSY;
  857. }
  858. thread->SetActivity(static_cast<ThreadActivity>(activity));
  859. system.PrepareReschedule(thread->GetProcessorID());
  860. return RESULT_SUCCESS;
  861. }
  862. /// Gets the thread context
  863. static ResultCode GetThreadContext(Core::System& system, VAddr thread_context, Handle handle) {
  864. LOG_DEBUG(Kernel_SVC, "called, context=0x{:08X}, thread=0x{:X}", thread_context, handle);
  865. const auto* current_process = system.Kernel().CurrentProcess();
  866. const std::shared_ptr<Thread> thread = current_process->GetHandleTable().Get<Thread>(handle);
  867. if (!thread) {
  868. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", handle);
  869. return ERR_INVALID_HANDLE;
  870. }
  871. if (thread->GetOwnerProcess() != current_process) {
  872. LOG_ERROR(Kernel_SVC,
  873. "The current process does not own the current thread, thread_handle={:08X} "
  874. "thread_pid={}, "
  875. "current_process_pid={}",
  876. handle, thread->GetOwnerProcess()->GetProcessID(),
  877. current_process->GetProcessID());
  878. return ERR_INVALID_HANDLE;
  879. }
  880. if (thread.get() == system.CurrentScheduler().GetCurrentThread()) {
  881. LOG_ERROR(Kernel_SVC, "The thread handle specified is the current running thread");
  882. return ERR_BUSY;
  883. }
  884. Core::ARM_Interface::ThreadContext64 ctx = thread->GetContext64();
  885. // Mask away mode bits, interrupt bits, IL bit, and other reserved bits.
  886. ctx.pstate &= 0xFF0FFE20;
  887. // If 64-bit, we can just write the context registers directly and we're good.
  888. // However, if 32-bit, we have to ensure some registers are zeroed out.
  889. if (!current_process->Is64BitProcess()) {
  890. std::fill(ctx.cpu_registers.begin() + 15, ctx.cpu_registers.end(), 0);
  891. std::fill(ctx.vector_registers.begin() + 16, ctx.vector_registers.end(), u128{});
  892. }
  893. system.Memory().WriteBlock(thread_context, &ctx, sizeof(ctx));
  894. return RESULT_SUCCESS;
  895. }
  896. /// Gets the priority for the specified thread
  897. static ResultCode GetThreadPriority(Core::System& system, u32* priority, Handle handle) {
  898. LOG_TRACE(Kernel_SVC, "called");
  899. const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
  900. const std::shared_ptr<Thread> thread = handle_table.Get<Thread>(handle);
  901. if (!thread) {
  902. *priority = 0;
  903. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", handle);
  904. return ERR_INVALID_HANDLE;
  905. }
  906. *priority = thread->GetPriority();
  907. return RESULT_SUCCESS;
  908. }
  909. static ResultCode GetThreadPriority32(Core::System& system, u32* priority, Handle handle) {
  910. return GetThreadPriority(system, priority, handle);
  911. }
  912. /// Sets the priority for the specified thread
  913. static ResultCode SetThreadPriority(Core::System& system, Handle handle, u32 priority) {
  914. LOG_TRACE(Kernel_SVC, "called");
  915. if (priority > THREADPRIO_LOWEST) {
  916. LOG_ERROR(
  917. Kernel_SVC,
  918. "An invalid priority was specified, expected {} but got {} for thread_handle={:08X}",
  919. THREADPRIO_LOWEST, priority, handle);
  920. return ERR_INVALID_THREAD_PRIORITY;
  921. }
  922. const auto* const current_process = system.Kernel().CurrentProcess();
  923. std::shared_ptr<Thread> thread = current_process->GetHandleTable().Get<Thread>(handle);
  924. if (!thread) {
  925. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", handle);
  926. return ERR_INVALID_HANDLE;
  927. }
  928. thread->SetPriority(priority);
  929. return RESULT_SUCCESS;
  930. }
  931. /// Get which CPU core is executing the current thread
  932. static u32 GetCurrentProcessorNumber(Core::System& system) {
  933. LOG_TRACE(Kernel_SVC, "called");
  934. return static_cast<u32>(system.CurrentPhysicalCore().CoreIndex());
  935. }
  936. static ResultCode MapSharedMemory(Core::System& system, Handle shared_memory_handle, VAddr addr,
  937. u64 size, u32 permissions) {
  938. LOG_TRACE(Kernel_SVC,
  939. "called, shared_memory_handle=0x{:X}, addr=0x{:X}, size=0x{:X}, permissions=0x{:08X}",
  940. shared_memory_handle, addr, size, permissions);
  941. if (!Common::Is4KBAligned(addr)) {
  942. LOG_ERROR(Kernel_SVC, "Address is not aligned to 4KB, addr=0x{:016X}", addr);
  943. return ERR_INVALID_ADDRESS;
  944. }
  945. if (size == 0) {
  946. LOG_ERROR(Kernel_SVC, "Size is 0");
  947. return ERR_INVALID_SIZE;
  948. }
  949. if (!Common::Is4KBAligned(size)) {
  950. LOG_ERROR(Kernel_SVC, "Size is not aligned to 4KB, size=0x{:016X}", size);
  951. return ERR_INVALID_SIZE;
  952. }
  953. if (!IsValidAddressRange(addr, size)) {
  954. LOG_ERROR(Kernel_SVC, "Region is not a valid address range, addr=0x{:016X}, size=0x{:016X}",
  955. addr, size);
  956. return ERR_INVALID_ADDRESS_STATE;
  957. }
  958. const auto permission_type = static_cast<Memory::MemoryPermission>(permissions);
  959. if ((permission_type | Memory::MemoryPermission::Write) !=
  960. Memory::MemoryPermission::ReadAndWrite) {
  961. LOG_ERROR(Kernel_SVC, "Expected Read or ReadWrite permission but got permissions=0x{:08X}",
  962. permissions);
  963. return ERR_INVALID_MEMORY_PERMISSIONS;
  964. }
  965. auto* const current_process{system.Kernel().CurrentProcess()};
  966. auto& page_table{current_process->PageTable()};
  967. if (page_table.IsInvalidRegion(addr, size)) {
  968. LOG_ERROR(Kernel_SVC,
  969. "Addr does not fit within the valid region, addr=0x{:016X}, "
  970. "size=0x{:016X}",
  971. addr, size);
  972. return ERR_INVALID_MEMORY_RANGE;
  973. }
  974. if (page_table.IsInsideHeapRegion(addr, size)) {
  975. LOG_ERROR(Kernel_SVC,
  976. "Addr does not fit within the heap region, addr=0x{:016X}, "
  977. "size=0x{:016X}",
  978. addr, size);
  979. return ERR_INVALID_MEMORY_RANGE;
  980. }
  981. if (page_table.IsInsideAliasRegion(addr, size)) {
  982. LOG_ERROR(Kernel_SVC,
  983. "Address does not fit within the map region, addr=0x{:016X}, "
  984. "size=0x{:016X}",
  985. addr, size);
  986. return ERR_INVALID_MEMORY_RANGE;
  987. }
  988. auto shared_memory{current_process->GetHandleTable().Get<SharedMemory>(shared_memory_handle)};
  989. if (!shared_memory) {
  990. LOG_ERROR(Kernel_SVC, "Shared memory does not exist, shared_memory_handle=0x{:08X}",
  991. shared_memory_handle);
  992. return ERR_INVALID_HANDLE;
  993. }
  994. return shared_memory->Map(*current_process, addr, size, permission_type);
  995. }
  996. static ResultCode QueryProcessMemory(Core::System& system, VAddr memory_info_address,
  997. VAddr page_info_address, Handle process_handle,
  998. VAddr address) {
  999. LOG_TRACE(Kernel_SVC, "called process=0x{:08X} address={:X}", process_handle, address);
  1000. const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
  1001. std::shared_ptr<Process> process = handle_table.Get<Process>(process_handle);
  1002. if (!process) {
  1003. LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}",
  1004. process_handle);
  1005. return ERR_INVALID_HANDLE;
  1006. }
  1007. auto& memory{system.Memory()};
  1008. const auto memory_info{process->PageTable().QueryInfo(address).GetSvcMemoryInfo()};
  1009. memory.Write64(memory_info_address + 0x00, memory_info.addr);
  1010. memory.Write64(memory_info_address + 0x08, memory_info.size);
  1011. memory.Write32(memory_info_address + 0x10, static_cast<u32>(memory_info.state) & 0xff);
  1012. memory.Write32(memory_info_address + 0x14, static_cast<u32>(memory_info.attr));
  1013. memory.Write32(memory_info_address + 0x18, static_cast<u32>(memory_info.perm));
  1014. memory.Write32(memory_info_address + 0x1c, memory_info.ipc_refcount);
  1015. memory.Write32(memory_info_address + 0x20, memory_info.device_refcount);
  1016. memory.Write32(memory_info_address + 0x24, 0);
  1017. // Page info appears to be currently unused by the kernel and is always set to zero.
  1018. memory.Write32(page_info_address, 0);
  1019. return RESULT_SUCCESS;
  1020. }
  1021. static ResultCode QueryMemory(Core::System& system, VAddr memory_info_address,
  1022. VAddr page_info_address, VAddr query_address) {
  1023. LOG_TRACE(Kernel_SVC,
  1024. "called, memory_info_address=0x{:016X}, page_info_address=0x{:016X}, "
  1025. "query_address=0x{:016X}",
  1026. memory_info_address, page_info_address, query_address);
  1027. return QueryProcessMemory(system, memory_info_address, page_info_address, CurrentProcess,
  1028. query_address);
  1029. }
  1030. static ResultCode QueryMemory32(Core::System& system, u32 memory_info_address,
  1031. u32 page_info_address, u32 query_address) {
  1032. return QueryMemory(system, memory_info_address, page_info_address, query_address);
  1033. }
  1034. static ResultCode MapProcessCodeMemory(Core::System& system, Handle process_handle, u64 dst_address,
  1035. u64 src_address, u64 size) {
  1036. LOG_DEBUG(Kernel_SVC,
  1037. "called. process_handle=0x{:08X}, dst_address=0x{:016X}, "
  1038. "src_address=0x{:016X}, size=0x{:016X}",
  1039. process_handle, dst_address, src_address, size);
  1040. if (!Common::Is4KBAligned(src_address)) {
  1041. LOG_ERROR(Kernel_SVC, "src_address is not page-aligned (src_address=0x{:016X}).",
  1042. src_address);
  1043. return ERR_INVALID_ADDRESS;
  1044. }
  1045. if (!Common::Is4KBAligned(dst_address)) {
  1046. LOG_ERROR(Kernel_SVC, "dst_address is not page-aligned (dst_address=0x{:016X}).",
  1047. dst_address);
  1048. return ERR_INVALID_ADDRESS;
  1049. }
  1050. if (size == 0 || !Common::Is4KBAligned(size)) {
  1051. LOG_ERROR(Kernel_SVC, "Size is zero or not page-aligned (size=0x{:016X})", size);
  1052. return ERR_INVALID_SIZE;
  1053. }
  1054. if (!IsValidAddressRange(dst_address, size)) {
  1055. LOG_ERROR(Kernel_SVC,
  1056. "Destination address range overflows the address space (dst_address=0x{:016X}, "
  1057. "size=0x{:016X}).",
  1058. dst_address, size);
  1059. return ERR_INVALID_ADDRESS_STATE;
  1060. }
  1061. if (!IsValidAddressRange(src_address, size)) {
  1062. LOG_ERROR(Kernel_SVC,
  1063. "Source address range overflows the address space (src_address=0x{:016X}, "
  1064. "size=0x{:016X}).",
  1065. src_address, size);
  1066. return ERR_INVALID_ADDRESS_STATE;
  1067. }
  1068. const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
  1069. auto process = handle_table.Get<Process>(process_handle);
  1070. if (!process) {
  1071. LOG_ERROR(Kernel_SVC, "Invalid process handle specified (handle=0x{:08X}).",
  1072. process_handle);
  1073. return ERR_INVALID_HANDLE;
  1074. }
  1075. auto& page_table = process->PageTable();
  1076. if (!page_table.IsInsideAddressSpace(src_address, size)) {
  1077. LOG_ERROR(Kernel_SVC,
  1078. "Source address range is not within the address space (src_address=0x{:016X}, "
  1079. "size=0x{:016X}).",
  1080. src_address, size);
  1081. return ERR_INVALID_ADDRESS_STATE;
  1082. }
  1083. if (!page_table.IsInsideASLRRegion(dst_address, size)) {
  1084. LOG_ERROR(Kernel_SVC,
  1085. "Destination address range is not within the ASLR region (dst_address=0x{:016X}, "
  1086. "size=0x{:016X}).",
  1087. dst_address, size);
  1088. return ERR_INVALID_MEMORY_RANGE;
  1089. }
  1090. return page_table.MapProcessCodeMemory(dst_address, src_address, size);
  1091. }
  1092. static ResultCode UnmapProcessCodeMemory(Core::System& system, Handle process_handle,
  1093. u64 dst_address, u64 src_address, u64 size) {
  1094. LOG_DEBUG(Kernel_SVC,
  1095. "called. process_handle=0x{:08X}, dst_address=0x{:016X}, src_address=0x{:016X}, "
  1096. "size=0x{:016X}",
  1097. process_handle, dst_address, src_address, size);
  1098. if (!Common::Is4KBAligned(dst_address)) {
  1099. LOG_ERROR(Kernel_SVC, "dst_address is not page-aligned (dst_address=0x{:016X}).",
  1100. dst_address);
  1101. return ERR_INVALID_ADDRESS;
  1102. }
  1103. if (!Common::Is4KBAligned(src_address)) {
  1104. LOG_ERROR(Kernel_SVC, "src_address is not page-aligned (src_address=0x{:016X}).",
  1105. src_address);
  1106. return ERR_INVALID_ADDRESS;
  1107. }
  1108. if (size == 0 || Common::Is4KBAligned(size)) {
  1109. LOG_ERROR(Kernel_SVC, "Size is zero or not page-aligned (size=0x{:016X}).", size);
  1110. return ERR_INVALID_SIZE;
  1111. }
  1112. if (!IsValidAddressRange(dst_address, size)) {
  1113. LOG_ERROR(Kernel_SVC,
  1114. "Destination address range overflows the address space (dst_address=0x{:016X}, "
  1115. "size=0x{:016X}).",
  1116. dst_address, size);
  1117. return ERR_INVALID_ADDRESS_STATE;
  1118. }
  1119. if (!IsValidAddressRange(src_address, size)) {
  1120. LOG_ERROR(Kernel_SVC,
  1121. "Source address range overflows the address space (src_address=0x{:016X}, "
  1122. "size=0x{:016X}).",
  1123. src_address, size);
  1124. return ERR_INVALID_ADDRESS_STATE;
  1125. }
  1126. const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
  1127. auto process = handle_table.Get<Process>(process_handle);
  1128. if (!process) {
  1129. LOG_ERROR(Kernel_SVC, "Invalid process handle specified (handle=0x{:08X}).",
  1130. process_handle);
  1131. return ERR_INVALID_HANDLE;
  1132. }
  1133. auto& page_table = process->PageTable();
  1134. if (!page_table.IsInsideAddressSpace(src_address, size)) {
  1135. LOG_ERROR(Kernel_SVC,
  1136. "Source address range is not within the address space (src_address=0x{:016X}, "
  1137. "size=0x{:016X}).",
  1138. src_address, size);
  1139. return ERR_INVALID_ADDRESS_STATE;
  1140. }
  1141. if (!page_table.IsInsideASLRRegion(dst_address, size)) {
  1142. LOG_ERROR(Kernel_SVC,
  1143. "Destination address range is not within the ASLR region (dst_address=0x{:016X}, "
  1144. "size=0x{:016X}).",
  1145. dst_address, size);
  1146. return ERR_INVALID_MEMORY_RANGE;
  1147. }
  1148. return page_table.UnmapProcessCodeMemory(dst_address, src_address, size);
  1149. }
  1150. /// Exits the current process
  1151. static void ExitProcess(Core::System& system) {
  1152. auto* current_process = system.Kernel().CurrentProcess();
  1153. LOG_INFO(Kernel_SVC, "Process {} exiting", current_process->GetProcessID());
  1154. ASSERT_MSG(current_process->GetStatus() == ProcessStatus::Running,
  1155. "Process has already exited");
  1156. current_process->PrepareForTermination();
  1157. // Kill the current thread
  1158. system.CurrentScheduler().GetCurrentThread()->Stop();
  1159. system.PrepareReschedule();
  1160. }
  1161. /// Creates a new thread
  1162. static ResultCode CreateThread(Core::System& system, Handle* out_handle, VAddr entry_point, u64 arg,
  1163. VAddr stack_top, u32 priority, s32 processor_id) {
  1164. LOG_DEBUG(Kernel_SVC,
  1165. "called entrypoint=0x{:08X}, arg=0x{:08X}, stacktop=0x{:08X}, "
  1166. "threadpriority=0x{:08X}, processorid=0x{:08X} : created handle=0x{:08X}",
  1167. entry_point, arg, stack_top, priority, processor_id, *out_handle);
  1168. auto* const current_process = system.Kernel().CurrentProcess();
  1169. if (processor_id == THREADPROCESSORID_IDEAL) {
  1170. // Set the target CPU to the one specified by the process.
  1171. processor_id = current_process->GetIdealCore();
  1172. ASSERT(processor_id != THREADPROCESSORID_IDEAL);
  1173. }
  1174. if (processor_id < THREADPROCESSORID_0 || processor_id > THREADPROCESSORID_3) {
  1175. LOG_ERROR(Kernel_SVC, "Invalid thread processor ID: {}", processor_id);
  1176. return ERR_INVALID_PROCESSOR_ID;
  1177. }
  1178. const u64 core_mask = current_process->GetCoreMask();
  1179. if ((core_mask | (1ULL << processor_id)) != core_mask) {
  1180. LOG_ERROR(Kernel_SVC, "Invalid thread core specified ({})", processor_id);
  1181. return ERR_INVALID_PROCESSOR_ID;
  1182. }
  1183. if (priority > THREADPRIO_LOWEST) {
  1184. LOG_ERROR(Kernel_SVC,
  1185. "Invalid thread priority specified ({}). Must be within the range 0-64",
  1186. priority);
  1187. return ERR_INVALID_THREAD_PRIORITY;
  1188. }
  1189. if (((1ULL << priority) & current_process->GetPriorityMask()) == 0) {
  1190. LOG_ERROR(Kernel_SVC, "Invalid thread priority specified ({})", priority);
  1191. return ERR_INVALID_THREAD_PRIORITY;
  1192. }
  1193. auto& kernel = system.Kernel();
  1194. ASSERT(kernel.CurrentProcess()->GetResourceLimit()->Reserve(ResourceType::Threads, 1));
  1195. ThreadType type = THREADTYPE_USER;
  1196. CASCADE_RESULT(std::shared_ptr<Thread> thread,
  1197. Thread::Create(system, type, "", entry_point, priority, arg, processor_id,
  1198. stack_top, current_process));
  1199. const auto new_thread_handle = current_process->GetHandleTable().Create(thread);
  1200. if (new_thread_handle.Failed()) {
  1201. LOG_ERROR(Kernel_SVC, "Failed to create handle with error=0x{:X}",
  1202. new_thread_handle.Code().raw);
  1203. return new_thread_handle.Code();
  1204. }
  1205. *out_handle = *new_thread_handle;
  1206. // Set the thread name for debugging purposes.
  1207. thread->SetName(
  1208. fmt::format("thread[entry_point={:X}, handle={:X}]", entry_point, *new_thread_handle));
  1209. system.PrepareReschedule(thread->GetProcessorID());
  1210. return RESULT_SUCCESS;
  1211. }
  1212. /// Starts the thread for the provided handle
  1213. static ResultCode StartThread(Core::System& system, Handle thread_handle) {
  1214. LOG_DEBUG(Kernel_SVC, "called thread=0x{:08X}", thread_handle);
  1215. const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
  1216. const std::shared_ptr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  1217. if (!thread) {
  1218. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, thread_handle=0x{:08X}",
  1219. thread_handle);
  1220. return ERR_INVALID_HANDLE;
  1221. }
  1222. ASSERT(thread->GetStatus() == ThreadStatus::Dormant);
  1223. return thread->Start();
  1224. }
  1225. /// Called when a thread exits
  1226. static void ExitThread(Core::System& system) {
  1227. LOG_DEBUG(Kernel_SVC, "called, pc=0x{:08X}", system.CurrentArmInterface().GetPC());
  1228. auto* const current_thread = system.CurrentScheduler().GetCurrentThread();
  1229. system.GlobalScheduler().RemoveThread(SharedFrom(current_thread));
  1230. current_thread->Stop();
  1231. }
  1232. /// Sleep the current thread
  1233. static void SleepThread(Core::System& system, s64 nanoseconds) {
  1234. LOG_DEBUG(Kernel_SVC, "called nanoseconds={}", nanoseconds);
  1235. enum class SleepType : s64 {
  1236. YieldWithoutLoadBalancing = 0,
  1237. YieldWithLoadBalancing = -1,
  1238. YieldAndWaitForLoadBalancing = -2,
  1239. };
  1240. auto& scheduler = system.CurrentScheduler();
  1241. auto* const current_thread = scheduler.GetCurrentThread();
  1242. bool is_redundant = false;
  1243. if (nanoseconds <= 0) {
  1244. switch (static_cast<SleepType>(nanoseconds)) {
  1245. case SleepType::YieldWithoutLoadBalancing:
  1246. current_thread->YieldSimple();
  1247. break;
  1248. case SleepType::YieldWithLoadBalancing:
  1249. current_thread->YieldAndBalanceLoad();
  1250. break;
  1251. case SleepType::YieldAndWaitForLoadBalancing:
  1252. current_thread->YieldAndWaitForLoadBalancing();
  1253. break;
  1254. default:
  1255. UNREACHABLE_MSG("Unimplemented sleep yield type '{:016X}'!", nanoseconds);
  1256. }
  1257. } else {
  1258. current_thread->Sleep(nanoseconds);
  1259. }
  1260. }
  1261. /// Wait process wide key atomic
  1262. static ResultCode WaitProcessWideKeyAtomic(Core::System& system, VAddr mutex_addr,
  1263. VAddr condition_variable_addr, Handle thread_handle,
  1264. s64 nano_seconds) {
  1265. LOG_TRACE(
  1266. Kernel_SVC,
  1267. "called mutex_addr={:X}, condition_variable_addr={:X}, thread_handle=0x{:08X}, timeout={}",
  1268. mutex_addr, condition_variable_addr, thread_handle, nano_seconds);
  1269. if (Core::Memory::IsKernelVirtualAddress(mutex_addr)) {
  1270. LOG_ERROR(
  1271. Kernel_SVC,
  1272. "Given mutex address must not be within the kernel address space. address=0x{:016X}",
  1273. mutex_addr);
  1274. return ERR_INVALID_ADDRESS_STATE;
  1275. }
  1276. if (!Common::IsWordAligned(mutex_addr)) {
  1277. LOG_ERROR(Kernel_SVC, "Given mutex address must be word-aligned. address=0x{:016X}",
  1278. mutex_addr);
  1279. return ERR_INVALID_ADDRESS;
  1280. }
  1281. ASSERT(condition_variable_addr == Common::AlignDown(condition_variable_addr, 4));
  1282. auto* const current_process = system.Kernel().CurrentProcess();
  1283. const auto& handle_table = current_process->GetHandleTable();
  1284. std::shared_ptr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  1285. ASSERT(thread);
  1286. const auto release_result = current_process->GetMutex().Release(mutex_addr);
  1287. if (release_result.IsError()) {
  1288. return release_result;
  1289. }
  1290. Thread* current_thread = system.CurrentScheduler().GetCurrentThread();
  1291. current_thread->SetCondVarWaitAddress(condition_variable_addr);
  1292. current_thread->SetMutexWaitAddress(mutex_addr);
  1293. current_thread->SetWaitHandle(thread_handle);
  1294. current_thread->SetStatus(ThreadStatus::WaitCondVar);
  1295. current_thread->InvalidateWakeupCallback();
  1296. current_process->InsertConditionVariableThread(SharedFrom(current_thread));
  1297. current_thread->WakeAfterDelay(nano_seconds);
  1298. // Note: Deliberately don't attempt to inherit the lock owner's priority.
  1299. system.PrepareReschedule(current_thread->GetProcessorID());
  1300. return RESULT_SUCCESS;
  1301. }
  1302. /// Signal process wide key
  1303. static void SignalProcessWideKey(Core::System& system, VAddr condition_variable_addr, s32 target) {
  1304. LOG_TRACE(Kernel_SVC, "called, condition_variable_addr=0x{:X}, target=0x{:08X}",
  1305. condition_variable_addr, target);
  1306. ASSERT(condition_variable_addr == Common::AlignDown(condition_variable_addr, 4));
  1307. // Retrieve a list of all threads that are waiting for this condition variable.
  1308. auto* const current_process = system.Kernel().CurrentProcess();
  1309. std::vector<std::shared_ptr<Thread>> waiting_threads =
  1310. current_process->GetConditionVariableThreads(condition_variable_addr);
  1311. // Only process up to 'target' threads, unless 'target' is less equal 0, in which case process
  1312. // them all.
  1313. std::size_t last = waiting_threads.size();
  1314. if (target > 0)
  1315. last = std::min(waiting_threads.size(), static_cast<std::size_t>(target));
  1316. for (std::size_t index = 0; index < last; ++index) {
  1317. auto& thread = waiting_threads[index];
  1318. ASSERT(thread->GetCondVarWaitAddress() == condition_variable_addr);
  1319. // liberate Cond Var Thread.
  1320. current_process->RemoveConditionVariableThread(thread);
  1321. thread->SetCondVarWaitAddress(0);
  1322. const std::size_t current_core = system.CurrentCoreIndex();
  1323. auto& monitor = system.Monitor();
  1324. auto& memory = system.Memory();
  1325. // Atomically read the value of the mutex.
  1326. u32 mutex_val = 0;
  1327. u32 update_val = 0;
  1328. const VAddr mutex_address = thread->GetMutexWaitAddress();
  1329. do {
  1330. monitor.SetExclusive(current_core, mutex_address);
  1331. // If the mutex is not yet acquired, acquire it.
  1332. mutex_val = memory.Read32(mutex_address);
  1333. if (mutex_val != 0) {
  1334. update_val = mutex_val | Mutex::MutexHasWaitersFlag;
  1335. } else {
  1336. update_val = thread->GetWaitHandle();
  1337. }
  1338. } while (!monitor.ExclusiveWrite32(current_core, mutex_address, update_val));
  1339. if (mutex_val == 0) {
  1340. // We were able to acquire the mutex, resume this thread.
  1341. ASSERT(thread->GetStatus() == ThreadStatus::WaitCondVar);
  1342. thread->ResumeFromWait();
  1343. auto* const lock_owner = thread->GetLockOwner();
  1344. if (lock_owner != nullptr) {
  1345. lock_owner->RemoveMutexWaiter(thread);
  1346. }
  1347. thread->SetLockOwner(nullptr);
  1348. thread->SetMutexWaitAddress(0);
  1349. thread->SetWaitHandle(0);
  1350. thread->SetWaitSynchronizationResult(RESULT_SUCCESS);
  1351. system.PrepareReschedule(thread->GetProcessorID());
  1352. } else {
  1353. // The mutex is already owned by some other thread, make this thread wait on it.
  1354. const Handle owner_handle = static_cast<Handle>(mutex_val & Mutex::MutexOwnerMask);
  1355. const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
  1356. auto owner = handle_table.Get<Thread>(owner_handle);
  1357. ASSERT(owner);
  1358. ASSERT(thread->GetStatus() == ThreadStatus::WaitCondVar);
  1359. thread->InvalidateWakeupCallback();
  1360. thread->SetStatus(ThreadStatus::WaitMutex);
  1361. owner->AddMutexWaiter(thread);
  1362. system.PrepareReschedule(thread->GetProcessorID());
  1363. }
  1364. }
  1365. }
  1366. static void SignalProcessWideKey32(Core::System& system, u32 condition_variable_addr, s32 target) {
  1367. SignalProcessWideKey(system, condition_variable_addr, target);
  1368. }
  1369. // Wait for an address (via Address Arbiter)
  1370. static ResultCode WaitForAddress(Core::System& system, VAddr address, u32 type, s32 value,
  1371. s64 timeout) {
  1372. LOG_TRACE(Kernel_SVC, "called, address=0x{:X}, type=0x{:X}, value=0x{:X}, timeout={}", address,
  1373. type, value, timeout);
  1374. // If the passed address is a kernel virtual address, return invalid memory state.
  1375. if (Core::Memory::IsKernelVirtualAddress(address)) {
  1376. LOG_ERROR(Kernel_SVC, "Address is a kernel virtual address, address={:016X}", address);
  1377. return ERR_INVALID_ADDRESS_STATE;
  1378. }
  1379. // If the address is not properly aligned to 4 bytes, return invalid address.
  1380. if (!Common::IsWordAligned(address)) {
  1381. LOG_ERROR(Kernel_SVC, "Address is not word aligned, address={:016X}", address);
  1382. return ERR_INVALID_ADDRESS;
  1383. }
  1384. const auto arbitration_type = static_cast<AddressArbiter::ArbitrationType>(type);
  1385. auto& address_arbiter = system.Kernel().CurrentProcess()->GetAddressArbiter();
  1386. const ResultCode result =
  1387. address_arbiter.WaitForAddress(address, arbitration_type, value, timeout);
  1388. if (result == RESULT_SUCCESS) {
  1389. system.PrepareReschedule();
  1390. }
  1391. return result;
  1392. }
  1393. // Signals to an address (via Address Arbiter)
  1394. static ResultCode SignalToAddress(Core::System& system, VAddr address, u32 type, s32 value,
  1395. s32 num_to_wake) {
  1396. LOG_TRACE(Kernel_SVC, "called, address=0x{:X}, type=0x{:X}, value=0x{:X}, num_to_wake=0x{:X}",
  1397. address, type, value, num_to_wake);
  1398. // If the passed address is a kernel virtual address, return invalid memory state.
  1399. if (Core::Memory::IsKernelVirtualAddress(address)) {
  1400. LOG_ERROR(Kernel_SVC, "Address is a kernel virtual address, address={:016X}", address);
  1401. return ERR_INVALID_ADDRESS_STATE;
  1402. }
  1403. // If the address is not properly aligned to 4 bytes, return invalid address.
  1404. if (!Common::IsWordAligned(address)) {
  1405. LOG_ERROR(Kernel_SVC, "Address is not word aligned, address={:016X}", address);
  1406. return ERR_INVALID_ADDRESS;
  1407. }
  1408. const auto signal_type = static_cast<AddressArbiter::SignalType>(type);
  1409. auto& address_arbiter = system.Kernel().CurrentProcess()->GetAddressArbiter();
  1410. return address_arbiter.SignalToAddress(address, signal_type, value, num_to_wake);
  1411. }
  1412. static void KernelDebug([[maybe_unused]] Core::System& system,
  1413. [[maybe_unused]] u32 kernel_debug_type, [[maybe_unused]] u64 param1,
  1414. [[maybe_unused]] u64 param2, [[maybe_unused]] u64 param3) {
  1415. // Intentionally do nothing, as this does nothing in released kernel binaries.
  1416. }
  1417. static void ChangeKernelTraceState([[maybe_unused]] Core::System& system,
  1418. [[maybe_unused]] u32 trace_state) {
  1419. // Intentionally do nothing, as this does nothing in released kernel binaries.
  1420. }
  1421. /// This returns the total CPU ticks elapsed since the CPU was powered-on
  1422. static u64 GetSystemTick(Core::System& system) {
  1423. LOG_TRACE(Kernel_SVC, "called");
  1424. auto& core_timing = system.CoreTiming();
  1425. // Returns the value of cntpct_el0 (https://switchbrew.org/wiki/SVC#svcGetSystemTick)
  1426. const u64 result{system.CoreTiming().GetClockTicks()};
  1427. return result;
  1428. }
  1429. /// Close a handle
  1430. static ResultCode CloseHandle(Core::System& system, Handle handle) {
  1431. LOG_TRACE(Kernel_SVC, "Closing handle 0x{:08X}", handle);
  1432. auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
  1433. return handle_table.Close(handle);
  1434. }
  1435. static ResultCode CloseHandle32(Core::System& system, Handle handle) {
  1436. return CloseHandle(system, handle);
  1437. }
  1438. /// Clears the signaled state of an event or process.
  1439. static ResultCode ResetSignal(Core::System& system, Handle handle) {
  1440. LOG_DEBUG(Kernel_SVC, "called handle 0x{:08X}", handle);
  1441. const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
  1442. auto event = handle_table.Get<ReadableEvent>(handle);
  1443. if (event) {
  1444. return event->Reset();
  1445. }
  1446. auto process = handle_table.Get<Process>(handle);
  1447. if (process) {
  1448. return process->ClearSignalState();
  1449. }
  1450. LOG_ERROR(Kernel_SVC, "Invalid handle (0x{:08X})", handle);
  1451. return ERR_INVALID_HANDLE;
  1452. }
  1453. /// Creates a TransferMemory object
  1454. static ResultCode CreateTransferMemory(Core::System& system, Handle* handle, VAddr addr, u64 size,
  1455. u32 permissions) {
  1456. LOG_DEBUG(Kernel_SVC, "called addr=0x{:X}, size=0x{:X}, perms=0x{:08X}", addr, size,
  1457. permissions);
  1458. if (!Common::Is4KBAligned(addr)) {
  1459. LOG_ERROR(Kernel_SVC, "Address ({:016X}) is not page aligned!", addr);
  1460. return ERR_INVALID_ADDRESS;
  1461. }
  1462. if (!Common::Is4KBAligned(size) || size == 0) {
  1463. LOG_ERROR(Kernel_SVC, "Size ({:016X}) is not page aligned or equal to zero!", size);
  1464. return ERR_INVALID_ADDRESS;
  1465. }
  1466. if (!IsValidAddressRange(addr, size)) {
  1467. LOG_ERROR(Kernel_SVC, "Address and size cause overflow! (address={:016X}, size={:016X})",
  1468. addr, size);
  1469. return ERR_INVALID_ADDRESS_STATE;
  1470. }
  1471. const auto perms{static_cast<Memory::MemoryPermission>(permissions)};
  1472. if (perms > Memory::MemoryPermission::ReadAndWrite ||
  1473. perms == Memory::MemoryPermission::Write) {
  1474. LOG_ERROR(Kernel_SVC, "Invalid memory permissions for transfer memory! (perms={:08X})",
  1475. permissions);
  1476. return ERR_INVALID_MEMORY_PERMISSIONS;
  1477. }
  1478. auto& kernel = system.Kernel();
  1479. auto transfer_mem_handle = TransferMemory::Create(kernel, system.Memory(), addr, size, perms);
  1480. if (const auto reserve_result{transfer_mem_handle->Reserve()}; reserve_result.IsError()) {
  1481. return reserve_result;
  1482. }
  1483. auto& handle_table = kernel.CurrentProcess()->GetHandleTable();
  1484. const auto result{handle_table.Create(std::move(transfer_mem_handle))};
  1485. if (result.Failed()) {
  1486. return result.Code();
  1487. }
  1488. *handle = *result;
  1489. return RESULT_SUCCESS;
  1490. }
  1491. static ResultCode GetThreadCoreMask(Core::System& system, Handle thread_handle, u32* core,
  1492. u64* mask) {
  1493. LOG_TRACE(Kernel_SVC, "called, handle=0x{:08X}", thread_handle);
  1494. const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
  1495. const std::shared_ptr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  1496. if (!thread) {
  1497. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, thread_handle=0x{:08X}",
  1498. thread_handle);
  1499. *core = 0;
  1500. *mask = 0;
  1501. return ERR_INVALID_HANDLE;
  1502. }
  1503. *core = thread->GetIdealCore();
  1504. *mask = thread->GetAffinityMask();
  1505. return RESULT_SUCCESS;
  1506. }
  1507. static ResultCode SetThreadCoreMask(Core::System& system, Handle thread_handle, u32 core,
  1508. u64 affinity_mask) {
  1509. LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, core=0x{:X}, affinity_mask=0x{:016X}",
  1510. thread_handle, core, affinity_mask);
  1511. const auto* const current_process = system.Kernel().CurrentProcess();
  1512. if (core == static_cast<u32>(THREADPROCESSORID_IDEAL)) {
  1513. const u8 ideal_cpu_core = current_process->GetIdealCore();
  1514. ASSERT(ideal_cpu_core != static_cast<u8>(THREADPROCESSORID_IDEAL));
  1515. // Set the target CPU to the ideal core specified by the process.
  1516. core = ideal_cpu_core;
  1517. affinity_mask = 1ULL << core;
  1518. } else {
  1519. const u64 core_mask = current_process->GetCoreMask();
  1520. if ((core_mask | affinity_mask) != core_mask) {
  1521. LOG_ERROR(
  1522. Kernel_SVC,
  1523. "Invalid processor ID specified (core_mask=0x{:08X}, affinity_mask=0x{:016X})",
  1524. core_mask, affinity_mask);
  1525. return ERR_INVALID_PROCESSOR_ID;
  1526. }
  1527. if (affinity_mask == 0) {
  1528. LOG_ERROR(Kernel_SVC, "Specfified affinity mask is zero.");
  1529. return ERR_INVALID_COMBINATION;
  1530. }
  1531. if (core < Core::NUM_CPU_CORES) {
  1532. if ((affinity_mask & (1ULL << core)) == 0) {
  1533. LOG_ERROR(Kernel_SVC,
  1534. "Core is not enabled for the current mask, core={}, mask={:016X}", core,
  1535. affinity_mask);
  1536. return ERR_INVALID_COMBINATION;
  1537. }
  1538. } else if (core != static_cast<u32>(THREADPROCESSORID_DONT_CARE) &&
  1539. core != static_cast<u32>(THREADPROCESSORID_DONT_UPDATE)) {
  1540. LOG_ERROR(Kernel_SVC, "Invalid processor ID specified (core={}).", core);
  1541. return ERR_INVALID_PROCESSOR_ID;
  1542. }
  1543. }
  1544. const auto& handle_table = current_process->GetHandleTable();
  1545. const std::shared_ptr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  1546. if (!thread) {
  1547. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, thread_handle=0x{:08X}",
  1548. thread_handle);
  1549. return ERR_INVALID_HANDLE;
  1550. }
  1551. return thread->SetCoreAndAffinityMask(core, affinity_mask);
  1552. }
  1553. static ResultCode CreateEvent(Core::System& system, Handle* write_handle, Handle* read_handle) {
  1554. LOG_DEBUG(Kernel_SVC, "called");
  1555. auto& kernel = system.Kernel();
  1556. const auto [readable_event, writable_event] =
  1557. WritableEvent::CreateEventPair(kernel, "CreateEvent");
  1558. HandleTable& handle_table = kernel.CurrentProcess()->GetHandleTable();
  1559. const auto write_create_result = handle_table.Create(writable_event);
  1560. if (write_create_result.Failed()) {
  1561. return write_create_result.Code();
  1562. }
  1563. *write_handle = *write_create_result;
  1564. const auto read_create_result = handle_table.Create(readable_event);
  1565. if (read_create_result.Failed()) {
  1566. handle_table.Close(*write_create_result);
  1567. return read_create_result.Code();
  1568. }
  1569. *read_handle = *read_create_result;
  1570. LOG_DEBUG(Kernel_SVC,
  1571. "successful. Writable event handle=0x{:08X}, Readable event handle=0x{:08X}",
  1572. *write_create_result, *read_create_result);
  1573. return RESULT_SUCCESS;
  1574. }
  1575. static ResultCode ClearEvent(Core::System& system, Handle handle) {
  1576. LOG_TRACE(Kernel_SVC, "called, event=0x{:08X}", handle);
  1577. const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
  1578. auto writable_event = handle_table.Get<WritableEvent>(handle);
  1579. if (writable_event) {
  1580. writable_event->Clear();
  1581. return RESULT_SUCCESS;
  1582. }
  1583. auto readable_event = handle_table.Get<ReadableEvent>(handle);
  1584. if (readable_event) {
  1585. readable_event->Clear();
  1586. return RESULT_SUCCESS;
  1587. }
  1588. LOG_ERROR(Kernel_SVC, "Event handle does not exist, handle=0x{:08X}", handle);
  1589. return ERR_INVALID_HANDLE;
  1590. }
  1591. static ResultCode SignalEvent(Core::System& system, Handle handle) {
  1592. LOG_DEBUG(Kernel_SVC, "called. Handle=0x{:08X}", handle);
  1593. HandleTable& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
  1594. auto writable_event = handle_table.Get<WritableEvent>(handle);
  1595. if (!writable_event) {
  1596. LOG_ERROR(Kernel_SVC, "Non-existent writable event handle used (0x{:08X})", handle);
  1597. return ERR_INVALID_HANDLE;
  1598. }
  1599. writable_event->Signal();
  1600. system.PrepareReschedule();
  1601. return RESULT_SUCCESS;
  1602. }
  1603. static ResultCode GetProcessInfo(Core::System& system, u64* out, Handle process_handle, u32 type) {
  1604. LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, type=0x{:X}", process_handle, type);
  1605. // This function currently only allows retrieving a process' status.
  1606. enum class InfoType {
  1607. Status,
  1608. };
  1609. const auto& handle_table = system.Kernel().CurrentProcess()->GetHandleTable();
  1610. const auto process = handle_table.Get<Process>(process_handle);
  1611. if (!process) {
  1612. LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}",
  1613. process_handle);
  1614. return ERR_INVALID_HANDLE;
  1615. }
  1616. const auto info_type = static_cast<InfoType>(type);
  1617. if (info_type != InfoType::Status) {
  1618. LOG_ERROR(Kernel_SVC, "Expected info_type to be Status but got {} instead", type);
  1619. return ERR_INVALID_ENUM_VALUE;
  1620. }
  1621. *out = static_cast<u64>(process->GetStatus());
  1622. return RESULT_SUCCESS;
  1623. }
  1624. static ResultCode CreateResourceLimit(Core::System& system, Handle* out_handle) {
  1625. LOG_DEBUG(Kernel_SVC, "called");
  1626. auto& kernel = system.Kernel();
  1627. auto resource_limit = ResourceLimit::Create(kernel);
  1628. auto* const current_process = kernel.CurrentProcess();
  1629. ASSERT(current_process != nullptr);
  1630. const auto handle = current_process->GetHandleTable().Create(std::move(resource_limit));
  1631. if (handle.Failed()) {
  1632. return handle.Code();
  1633. }
  1634. *out_handle = *handle;
  1635. return RESULT_SUCCESS;
  1636. }
  1637. static ResultCode GetResourceLimitLimitValue(Core::System& system, u64* out_value,
  1638. Handle resource_limit, u32 resource_type) {
  1639. LOG_DEBUG(Kernel_SVC, "called. Handle={:08X}, Resource type={}", resource_limit, resource_type);
  1640. const auto limit_value = RetrieveResourceLimitValue(system, resource_limit, resource_type,
  1641. ResourceLimitValueType::LimitValue);
  1642. if (limit_value.Failed()) {
  1643. return limit_value.Code();
  1644. }
  1645. *out_value = static_cast<u64>(*limit_value);
  1646. return RESULT_SUCCESS;
  1647. }
  1648. static ResultCode GetResourceLimitCurrentValue(Core::System& system, u64* out_value,
  1649. Handle resource_limit, u32 resource_type) {
  1650. LOG_DEBUG(Kernel_SVC, "called. Handle={:08X}, Resource type={}", resource_limit, resource_type);
  1651. const auto current_value = RetrieveResourceLimitValue(system, resource_limit, resource_type,
  1652. ResourceLimitValueType::CurrentValue);
  1653. if (current_value.Failed()) {
  1654. return current_value.Code();
  1655. }
  1656. *out_value = static_cast<u64>(*current_value);
  1657. return RESULT_SUCCESS;
  1658. }
  1659. static ResultCode SetResourceLimitLimitValue(Core::System& system, Handle resource_limit,
  1660. u32 resource_type, u64 value) {
  1661. LOG_DEBUG(Kernel_SVC, "called. Handle={:08X}, Resource type={}, Value={}", resource_limit,
  1662. resource_type, value);
  1663. const auto type = static_cast<ResourceType>(resource_type);
  1664. if (!IsValidResourceType(type)) {
  1665. LOG_ERROR(Kernel_SVC, "Invalid resource limit type: '{}'", resource_type);
  1666. return ERR_INVALID_ENUM_VALUE;
  1667. }
  1668. auto* const current_process = system.Kernel().CurrentProcess();
  1669. ASSERT(current_process != nullptr);
  1670. auto resource_limit_object =
  1671. current_process->GetHandleTable().Get<ResourceLimit>(resource_limit);
  1672. if (!resource_limit_object) {
  1673. LOG_ERROR(Kernel_SVC, "Handle to non-existent resource limit instance used. Handle={:08X}",
  1674. resource_limit);
  1675. return ERR_INVALID_HANDLE;
  1676. }
  1677. const auto set_result = resource_limit_object->SetLimitValue(type, static_cast<s64>(value));
  1678. if (set_result.IsError()) {
  1679. LOG_ERROR(
  1680. Kernel_SVC,
  1681. "Attempted to lower resource limit ({}) for category '{}' below its current value ({})",
  1682. resource_limit_object->GetMaxResourceValue(type), resource_type,
  1683. resource_limit_object->GetCurrentResourceValue(type));
  1684. return set_result;
  1685. }
  1686. return RESULT_SUCCESS;
  1687. }
  1688. static ResultCode GetProcessList(Core::System& system, u32* out_num_processes,
  1689. VAddr out_process_ids, u32 out_process_ids_size) {
  1690. LOG_DEBUG(Kernel_SVC, "called. out_process_ids=0x{:016X}, out_process_ids_size={}",
  1691. out_process_ids, out_process_ids_size);
  1692. // If the supplied size is negative or greater than INT32_MAX / sizeof(u64), bail.
  1693. if ((out_process_ids_size & 0xF0000000) != 0) {
  1694. LOG_ERROR(Kernel_SVC,
  1695. "Supplied size outside [0, 0x0FFFFFFF] range. out_process_ids_size={}",
  1696. out_process_ids_size);
  1697. return ERR_OUT_OF_RANGE;
  1698. }
  1699. const auto& kernel = system.Kernel();
  1700. const auto total_copy_size = out_process_ids_size * sizeof(u64);
  1701. if (out_process_ids_size > 0 && !kernel.CurrentProcess()->PageTable().IsInsideAddressSpace(
  1702. out_process_ids, total_copy_size)) {
  1703. LOG_ERROR(Kernel_SVC, "Address range outside address space. begin=0x{:016X}, end=0x{:016X}",
  1704. out_process_ids, out_process_ids + total_copy_size);
  1705. return ERR_INVALID_ADDRESS_STATE;
  1706. }
  1707. auto& memory = system.Memory();
  1708. const auto& process_list = kernel.GetProcessList();
  1709. const auto num_processes = process_list.size();
  1710. const auto copy_amount = std::min(std::size_t{out_process_ids_size}, num_processes);
  1711. for (std::size_t i = 0; i < copy_amount; ++i) {
  1712. memory.Write64(out_process_ids, process_list[i]->GetProcessID());
  1713. out_process_ids += sizeof(u64);
  1714. }
  1715. *out_num_processes = static_cast<u32>(num_processes);
  1716. return RESULT_SUCCESS;
  1717. }
  1718. static ResultCode GetThreadList(Core::System& system, u32* out_num_threads, VAddr out_thread_ids,
  1719. u32 out_thread_ids_size, Handle debug_handle) {
  1720. // TODO: Handle this case when debug events are supported.
  1721. UNIMPLEMENTED_IF(debug_handle != InvalidHandle);
  1722. LOG_DEBUG(Kernel_SVC, "called. out_thread_ids=0x{:016X}, out_thread_ids_size={}",
  1723. out_thread_ids, out_thread_ids_size);
  1724. // If the size is negative or larger than INT32_MAX / sizeof(u64)
  1725. if ((out_thread_ids_size & 0xF0000000) != 0) {
  1726. LOG_ERROR(Kernel_SVC, "Supplied size outside [0, 0x0FFFFFFF] range. size={}",
  1727. out_thread_ids_size);
  1728. return ERR_OUT_OF_RANGE;
  1729. }
  1730. const auto* const current_process = system.Kernel().CurrentProcess();
  1731. const auto total_copy_size = out_thread_ids_size * sizeof(u64);
  1732. if (out_thread_ids_size > 0 &&
  1733. !current_process->PageTable().IsInsideAddressSpace(out_thread_ids, total_copy_size)) {
  1734. LOG_ERROR(Kernel_SVC, "Address range outside address space. begin=0x{:016X}, end=0x{:016X}",
  1735. out_thread_ids, out_thread_ids + total_copy_size);
  1736. return ERR_INVALID_ADDRESS_STATE;
  1737. }
  1738. auto& memory = system.Memory();
  1739. const auto& thread_list = current_process->GetThreadList();
  1740. const auto num_threads = thread_list.size();
  1741. const auto copy_amount = std::min(std::size_t{out_thread_ids_size}, num_threads);
  1742. auto list_iter = thread_list.cbegin();
  1743. for (std::size_t i = 0; i < copy_amount; ++i, ++list_iter) {
  1744. memory.Write64(out_thread_ids, (*list_iter)->GetThreadID());
  1745. out_thread_ids += sizeof(u64);
  1746. }
  1747. *out_num_threads = static_cast<u32>(num_threads);
  1748. return RESULT_SUCCESS;
  1749. }
  1750. namespace {
  1751. struct FunctionDef {
  1752. using Func = void(Core::System&);
  1753. u32 id;
  1754. Func* func;
  1755. const char* name;
  1756. };
  1757. } // namespace
  1758. static const FunctionDef SVC_Table_32[] = {
  1759. {0x00, nullptr, "Unknown"},
  1760. {0x01, SvcWrap32<SetHeapSize32>, "SetHeapSize32"},
  1761. {0x02, nullptr, "Unknown"},
  1762. {0x03, nullptr, "SetMemoryAttribute32"},
  1763. {0x04, nullptr, "MapMemory32"},
  1764. {0x05, nullptr, "UnmapMemory32"},
  1765. {0x06, SvcWrap32<QueryMemory32>, "QueryMemory32"},
  1766. {0x07, nullptr, "ExitProcess32"},
  1767. {0x08, nullptr, "CreateThread32"},
  1768. {0x09, nullptr, "StartThread32"},
  1769. {0x0a, nullptr, "ExitThread32"},
  1770. {0x0b, nullptr, "SleepThread32"},
  1771. {0x0c, SvcWrap32<GetThreadPriority32>, "GetThreadPriority32"},
  1772. {0x0d, nullptr, "SetThreadPriority32"},
  1773. {0x0e, nullptr, "GetThreadCoreMask32"},
  1774. {0x0f, nullptr, "SetThreadCoreMask32"},
  1775. {0x10, nullptr, "GetCurrentProcessorNumber32"},
  1776. {0x11, nullptr, "SignalEvent32"},
  1777. {0x12, nullptr, "ClearEvent32"},
  1778. {0x13, nullptr, "MapSharedMemory32"},
  1779. {0x14, nullptr, "UnmapSharedMemory32"},
  1780. {0x15, nullptr, "CreateTransferMemory32"},
  1781. {0x16, SvcWrap32<CloseHandle32>, "CloseHandle32"},
  1782. {0x17, nullptr, "ResetSignal32"},
  1783. {0x18, SvcWrap32<WaitSynchronization32>, "WaitSynchronization32"},
  1784. {0x19, nullptr, "CancelSynchronization32"},
  1785. {0x1a, nullptr, "ArbitrateLock32"},
  1786. {0x1b, nullptr, "ArbitrateUnlock32"},
  1787. {0x1c, nullptr, "WaitProcessWideKeyAtomic32"},
  1788. {0x1d, SvcWrap32<SignalProcessWideKey32>, "SignalProcessWideKey32"},
  1789. {0x1e, nullptr, "GetSystemTick32"},
  1790. {0x1f, SvcWrap32<ConnectToNamedPort32>, "ConnectToNamedPort32"},
  1791. {0x20, nullptr, "Unknown"},
  1792. {0x21, SvcWrap32<SendSyncRequest32>, "SendSyncRequest32"},
  1793. {0x22, nullptr, "SendSyncRequestWithUserBuffer32"},
  1794. {0x23, nullptr, "Unknown"},
  1795. {0x24, nullptr, "GetProcessId32"},
  1796. {0x25, SvcWrap32<GetThreadId32>, "GetThreadId32"},
  1797. {0x26, nullptr, "Break32"},
  1798. {0x27, nullptr, "OutputDebugString32"},
  1799. {0x28, nullptr, "Unknown"},
  1800. {0x29, SvcWrap32<GetInfo32>, "GetInfo32"},
  1801. {0x2a, nullptr, "Unknown"},
  1802. {0x2b, nullptr, "Unknown"},
  1803. {0x2c, nullptr, "MapPhysicalMemory32"},
  1804. {0x2d, nullptr, "UnmapPhysicalMemory32"},
  1805. {0x2e, nullptr, "Unknown"},
  1806. {0x2f, nullptr, "Unknown"},
  1807. {0x30, nullptr, "Unknown"},
  1808. {0x31, nullptr, "Unknown"},
  1809. {0x32, nullptr, "SetThreadActivity32"},
  1810. {0x33, nullptr, "GetThreadContext32"},
  1811. {0x34, nullptr, "WaitForAddress32"},
  1812. {0x35, nullptr, "SignalToAddress32"},
  1813. {0x36, nullptr, "Unknown"},
  1814. {0x37, nullptr, "Unknown"},
  1815. {0x38, nullptr, "Unknown"},
  1816. {0x39, nullptr, "Unknown"},
  1817. {0x3a, nullptr, "Unknown"},
  1818. {0x3b, nullptr, "Unknown"},
  1819. {0x3c, nullptr, "Unknown"},
  1820. {0x3d, nullptr, "Unknown"},
  1821. {0x3e, nullptr, "Unknown"},
  1822. {0x3f, nullptr, "Unknown"},
  1823. {0x40, nullptr, "CreateSession32"},
  1824. {0x41, nullptr, "AcceptSession32"},
  1825. {0x42, nullptr, "Unknown"},
  1826. {0x43, nullptr, "ReplyAndReceive32"},
  1827. {0x44, nullptr, "Unknown"},
  1828. {0x45, nullptr, "CreateEvent32"},
  1829. {0x46, nullptr, "Unknown"},
  1830. {0x47, nullptr, "Unknown"},
  1831. {0x48, nullptr, "Unknown"},
  1832. {0x49, nullptr, "Unknown"},
  1833. {0x4a, nullptr, "Unknown"},
  1834. {0x4b, nullptr, "Unknown"},
  1835. {0x4c, nullptr, "Unknown"},
  1836. {0x4d, nullptr, "Unknown"},
  1837. {0x4e, nullptr, "Unknown"},
  1838. {0x4f, nullptr, "Unknown"},
  1839. {0x50, nullptr, "Unknown"},
  1840. {0x51, nullptr, "Unknown"},
  1841. {0x52, nullptr, "Unknown"},
  1842. {0x53, nullptr, "Unknown"},
  1843. {0x54, nullptr, "Unknown"},
  1844. {0x55, nullptr, "Unknown"},
  1845. {0x56, nullptr, "Unknown"},
  1846. {0x57, nullptr, "Unknown"},
  1847. {0x58, nullptr, "Unknown"},
  1848. {0x59, nullptr, "Unknown"},
  1849. {0x5a, nullptr, "Unknown"},
  1850. {0x5b, nullptr, "Unknown"},
  1851. {0x5c, nullptr, "Unknown"},
  1852. {0x5d, nullptr, "Unknown"},
  1853. {0x5e, nullptr, "Unknown"},
  1854. {0x5F, nullptr, "FlushProcessDataCache32"},
  1855. {0x60, nullptr, "Unknown"},
  1856. {0x61, nullptr, "Unknown"},
  1857. {0x62, nullptr, "Unknown"},
  1858. {0x63, nullptr, "Unknown"},
  1859. {0x64, nullptr, "Unknown"},
  1860. {0x65, nullptr, "GetProcessList32"},
  1861. {0x66, nullptr, "Unknown"},
  1862. {0x67, nullptr, "Unknown"},
  1863. {0x68, nullptr, "Unknown"},
  1864. {0x69, nullptr, "Unknown"},
  1865. {0x6A, nullptr, "Unknown"},
  1866. {0x6B, nullptr, "Unknown"},
  1867. {0x6C, nullptr, "Unknown"},
  1868. {0x6D, nullptr, "Unknown"},
  1869. {0x6E, nullptr, "Unknown"},
  1870. {0x6f, nullptr, "GetSystemInfo32"},
  1871. {0x70, nullptr, "CreatePort32"},
  1872. {0x71, nullptr, "ManageNamedPort32"},
  1873. {0x72, nullptr, "ConnectToPort32"},
  1874. {0x73, nullptr, "SetProcessMemoryPermission32"},
  1875. {0x74, nullptr, "Unknown"},
  1876. {0x75, nullptr, "Unknown"},
  1877. {0x76, nullptr, "Unknown"},
  1878. {0x77, nullptr, "MapProcessCodeMemory32"},
  1879. {0x78, nullptr, "UnmapProcessCodeMemory32"},
  1880. {0x79, nullptr, "Unknown"},
  1881. {0x7A, nullptr, "Unknown"},
  1882. {0x7B, nullptr, "TerminateProcess32"},
  1883. };
  1884. static const FunctionDef SVC_Table_64[] = {
  1885. {0x00, nullptr, "Unknown"},
  1886. {0x01, SvcWrap64<SetHeapSize>, "SetHeapSize"},
  1887. {0x02, nullptr, "SetMemoryPermission"},
  1888. {0x03, SvcWrap64<SetMemoryAttribute>, "SetMemoryAttribute"},
  1889. {0x04, SvcWrap64<MapMemory>, "MapMemory"},
  1890. {0x05, SvcWrap64<UnmapMemory>, "UnmapMemory"},
  1891. {0x06, SvcWrap64<QueryMemory>, "QueryMemory"},
  1892. {0x07, SvcWrap64<ExitProcess>, "ExitProcess"},
  1893. {0x08, SvcWrap64<CreateThread>, "CreateThread"},
  1894. {0x09, SvcWrap64<StartThread>, "StartThread"},
  1895. {0x0A, SvcWrap64<ExitThread>, "ExitThread"},
  1896. {0x0B, SvcWrap64<SleepThread>, "SleepThread"},
  1897. {0x0C, SvcWrap64<GetThreadPriority>, "GetThreadPriority"},
  1898. {0x0D, SvcWrap64<SetThreadPriority>, "SetThreadPriority"},
  1899. {0x0E, SvcWrap64<GetThreadCoreMask>, "GetThreadCoreMask"},
  1900. {0x0F, SvcWrap64<SetThreadCoreMask>, "SetThreadCoreMask"},
  1901. {0x10, SvcWrap64<GetCurrentProcessorNumber>, "GetCurrentProcessorNumber"},
  1902. {0x11, SvcWrap64<SignalEvent>, "SignalEvent"},
  1903. {0x12, SvcWrap64<ClearEvent>, "ClearEvent"},
  1904. {0x13, SvcWrap64<MapSharedMemory>, "MapSharedMemory"},
  1905. {0x14, nullptr, "UnmapSharedMemory"},
  1906. {0x15, SvcWrap64<CreateTransferMemory>, "CreateTransferMemory"},
  1907. {0x16, SvcWrap64<CloseHandle>, "CloseHandle"},
  1908. {0x17, SvcWrap64<ResetSignal>, "ResetSignal"},
  1909. {0x18, SvcWrap64<WaitSynchronization>, "WaitSynchronization"},
  1910. {0x19, SvcWrap64<CancelSynchronization>, "CancelSynchronization"},
  1911. {0x1A, SvcWrap64<ArbitrateLock>, "ArbitrateLock"},
  1912. {0x1B, SvcWrap64<ArbitrateUnlock>, "ArbitrateUnlock"},
  1913. {0x1C, SvcWrap64<WaitProcessWideKeyAtomic>, "WaitProcessWideKeyAtomic"},
  1914. {0x1D, SvcWrap64<SignalProcessWideKey>, "SignalProcessWideKey"},
  1915. {0x1E, SvcWrap64<GetSystemTick>, "GetSystemTick"},
  1916. {0x1F, SvcWrap64<ConnectToNamedPort>, "ConnectToNamedPort"},
  1917. {0x20, nullptr, "SendSyncRequestLight"},
  1918. {0x21, SvcWrap64<SendSyncRequest>, "SendSyncRequest"},
  1919. {0x22, nullptr, "SendSyncRequestWithUserBuffer"},
  1920. {0x23, nullptr, "SendAsyncRequestWithUserBuffer"},
  1921. {0x24, SvcWrap64<GetProcessId>, "GetProcessId"},
  1922. {0x25, SvcWrap64<GetThreadId>, "GetThreadId"},
  1923. {0x26, SvcWrap64<Break>, "Break"},
  1924. {0x27, SvcWrap64<OutputDebugString>, "OutputDebugString"},
  1925. {0x28, nullptr, "ReturnFromException"},
  1926. {0x29, SvcWrap64<GetInfo>, "GetInfo"},
  1927. {0x2A, nullptr, "FlushEntireDataCache"},
  1928. {0x2B, nullptr, "FlushDataCache"},
  1929. {0x2C, SvcWrap64<MapPhysicalMemory>, "MapPhysicalMemory"},
  1930. {0x2D, SvcWrap64<UnmapPhysicalMemory>, "UnmapPhysicalMemory"},
  1931. {0x2E, nullptr, "GetFutureThreadInfo"},
  1932. {0x2F, nullptr, "GetLastThreadInfo"},
  1933. {0x30, SvcWrap64<GetResourceLimitLimitValue>, "GetResourceLimitLimitValue"},
  1934. {0x31, SvcWrap64<GetResourceLimitCurrentValue>, "GetResourceLimitCurrentValue"},
  1935. {0x32, SvcWrap64<SetThreadActivity>, "SetThreadActivity"},
  1936. {0x33, SvcWrap64<GetThreadContext>, "GetThreadContext"},
  1937. {0x34, SvcWrap64<WaitForAddress>, "WaitForAddress"},
  1938. {0x35, SvcWrap64<SignalToAddress>, "SignalToAddress"},
  1939. {0x36, nullptr, "SynchronizePreemptionState"},
  1940. {0x37, nullptr, "Unknown"},
  1941. {0x38, nullptr, "Unknown"},
  1942. {0x39, nullptr, "Unknown"},
  1943. {0x3A, nullptr, "Unknown"},
  1944. {0x3B, nullptr, "Unknown"},
  1945. {0x3C, SvcWrap64<KernelDebug>, "KernelDebug"},
  1946. {0x3D, SvcWrap64<ChangeKernelTraceState>, "ChangeKernelTraceState"},
  1947. {0x3E, nullptr, "Unknown"},
  1948. {0x3F, nullptr, "Unknown"},
  1949. {0x40, nullptr, "CreateSession"},
  1950. {0x41, nullptr, "AcceptSession"},
  1951. {0x42, nullptr, "ReplyAndReceiveLight"},
  1952. {0x43, nullptr, "ReplyAndReceive"},
  1953. {0x44, nullptr, "ReplyAndReceiveWithUserBuffer"},
  1954. {0x45, SvcWrap64<CreateEvent>, "CreateEvent"},
  1955. {0x46, nullptr, "Unknown"},
  1956. {0x47, nullptr, "Unknown"},
  1957. {0x48, nullptr, "MapPhysicalMemoryUnsafe"},
  1958. {0x49, nullptr, "UnmapPhysicalMemoryUnsafe"},
  1959. {0x4A, nullptr, "SetUnsafeLimit"},
  1960. {0x4B, nullptr, "CreateCodeMemory"},
  1961. {0x4C, nullptr, "ControlCodeMemory"},
  1962. {0x4D, nullptr, "SleepSystem"},
  1963. {0x4E, nullptr, "ReadWriteRegister"},
  1964. {0x4F, nullptr, "SetProcessActivity"},
  1965. {0x50, nullptr, "CreateSharedMemory"},
  1966. {0x51, nullptr, "MapTransferMemory"},
  1967. {0x52, nullptr, "UnmapTransferMemory"},
  1968. {0x53, nullptr, "CreateInterruptEvent"},
  1969. {0x54, nullptr, "QueryPhysicalAddress"},
  1970. {0x55, nullptr, "QueryIoMapping"},
  1971. {0x56, nullptr, "CreateDeviceAddressSpace"},
  1972. {0x57, nullptr, "AttachDeviceAddressSpace"},
  1973. {0x58, nullptr, "DetachDeviceAddressSpace"},
  1974. {0x59, nullptr, "MapDeviceAddressSpaceByForce"},
  1975. {0x5A, nullptr, "MapDeviceAddressSpaceAligned"},
  1976. {0x5B, nullptr, "MapDeviceAddressSpace"},
  1977. {0x5C, nullptr, "UnmapDeviceAddressSpace"},
  1978. {0x5D, nullptr, "InvalidateProcessDataCache"},
  1979. {0x5E, nullptr, "StoreProcessDataCache"},
  1980. {0x5F, nullptr, "FlushProcessDataCache"},
  1981. {0x60, nullptr, "DebugActiveProcess"},
  1982. {0x61, nullptr, "BreakDebugProcess"},
  1983. {0x62, nullptr, "TerminateDebugProcess"},
  1984. {0x63, nullptr, "GetDebugEvent"},
  1985. {0x64, nullptr, "ContinueDebugEvent"},
  1986. {0x65, SvcWrap64<GetProcessList>, "GetProcessList"},
  1987. {0x66, SvcWrap64<GetThreadList>, "GetThreadList"},
  1988. {0x67, nullptr, "GetDebugThreadContext"},
  1989. {0x68, nullptr, "SetDebugThreadContext"},
  1990. {0x69, nullptr, "QueryDebugProcessMemory"},
  1991. {0x6A, nullptr, "ReadDebugProcessMemory"},
  1992. {0x6B, nullptr, "WriteDebugProcessMemory"},
  1993. {0x6C, nullptr, "SetHardwareBreakPoint"},
  1994. {0x6D, nullptr, "GetDebugThreadParam"},
  1995. {0x6E, nullptr, "Unknown"},
  1996. {0x6F, nullptr, "GetSystemInfo"},
  1997. {0x70, nullptr, "CreatePort"},
  1998. {0x71, nullptr, "ManageNamedPort"},
  1999. {0x72, nullptr, "ConnectToPort"},
  2000. {0x73, nullptr, "SetProcessMemoryPermission"},
  2001. {0x74, nullptr, "MapProcessMemory"},
  2002. {0x75, nullptr, "UnmapProcessMemory"},
  2003. {0x76, SvcWrap64<QueryProcessMemory>, "QueryProcessMemory"},
  2004. {0x77, SvcWrap64<MapProcessCodeMemory>, "MapProcessCodeMemory"},
  2005. {0x78, SvcWrap64<UnmapProcessCodeMemory>, "UnmapProcessCodeMemory"},
  2006. {0x79, nullptr, "CreateProcess"},
  2007. {0x7A, nullptr, "StartProcess"},
  2008. {0x7B, nullptr, "TerminateProcess"},
  2009. {0x7C, SvcWrap64<GetProcessInfo>, "GetProcessInfo"},
  2010. {0x7D, SvcWrap64<CreateResourceLimit>, "CreateResourceLimit"},
  2011. {0x7E, SvcWrap64<SetResourceLimitLimitValue>, "SetResourceLimitLimitValue"},
  2012. {0x7F, nullptr, "CallSecureMonitor"},
  2013. };
  2014. static const FunctionDef* GetSVCInfo32(u32 func_num) {
  2015. if (func_num >= std::size(SVC_Table_32)) {
  2016. LOG_ERROR(Kernel_SVC, "Unknown svc=0x{:02X}", func_num);
  2017. return nullptr;
  2018. }
  2019. return &SVC_Table_32[func_num];
  2020. }
  2021. static const FunctionDef* GetSVCInfo64(u32 func_num) {
  2022. if (func_num >= std::size(SVC_Table_64)) {
  2023. LOG_ERROR(Kernel_SVC, "Unknown svc=0x{:02X}", func_num);
  2024. return nullptr;
  2025. }
  2026. return &SVC_Table_64[func_num];
  2027. }
  2028. MICROPROFILE_DEFINE(Kernel_SVC, "Kernel", "SVC", MP_RGB(70, 200, 70));
  2029. void Call(Core::System& system, u32 immediate) {
  2030. MICROPROFILE_SCOPE(Kernel_SVC);
  2031. const FunctionDef* info = system.CurrentProcess()->Is64BitProcess() ? GetSVCInfo64(immediate)
  2032. : GetSVCInfo32(immediate);
  2033. if (info) {
  2034. if (info->func) {
  2035. info->func(system);
  2036. } else {
  2037. LOG_CRITICAL(Kernel_SVC, "Unimplemented SVC function {}(..)", info->name);
  2038. }
  2039. } else {
  2040. LOG_CRITICAL(Kernel_SVC, "Unknown SVC function 0x{:X}", immediate);
  2041. }
  2042. }
  2043. } // namespace Kernel::Svc