svc.cpp 93 KB

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