svc.cpp 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cinttypes>
  6. #include <iterator>
  7. #include <mutex>
  8. #include <vector>
  9. #include "common/alignment.h"
  10. #include "common/assert.h"
  11. #include "common/logging/log.h"
  12. #include "common/microprofile.h"
  13. #include "common/string_util.h"
  14. #include "core/arm/exclusive_monitor.h"
  15. #include "core/core.h"
  16. #include "core/core_cpu.h"
  17. #include "core/core_timing.h"
  18. #include "core/hle/kernel/address_arbiter.h"
  19. #include "core/hle/kernel/client_port.h"
  20. #include "core/hle/kernel/client_session.h"
  21. #include "core/hle/kernel/handle_table.h"
  22. #include "core/hle/kernel/kernel.h"
  23. #include "core/hle/kernel/mutex.h"
  24. #include "core/hle/kernel/process.h"
  25. #include "core/hle/kernel/readable_event.h"
  26. #include "core/hle/kernel/resource_limit.h"
  27. #include "core/hle/kernel/scheduler.h"
  28. #include "core/hle/kernel/shared_memory.h"
  29. #include "core/hle/kernel/svc.h"
  30. #include "core/hle/kernel/svc_wrap.h"
  31. #include "core/hle/kernel/thread.h"
  32. #include "core/hle/kernel/writable_event.h"
  33. #include "core/hle/lock.h"
  34. #include "core/hle/result.h"
  35. #include "core/hle/service/service.h"
  36. namespace Kernel {
  37. namespace {
  38. // Checks if address + size is greater than the given address
  39. // This can return false if the size causes an overflow of a 64-bit type
  40. // or if the given size is zero.
  41. constexpr bool IsValidAddressRange(VAddr address, u64 size) {
  42. return address + size > address;
  43. }
  44. // Checks if a given address range lies within a larger address range.
  45. constexpr bool IsInsideAddressRange(VAddr address, u64 size, VAddr address_range_begin,
  46. VAddr address_range_end) {
  47. const VAddr end_address = address + size - 1;
  48. return address_range_begin <= address && end_address <= address_range_end - 1;
  49. }
  50. bool IsInsideAddressSpace(const VMManager& vm, VAddr address, u64 size) {
  51. return IsInsideAddressRange(address, size, vm.GetAddressSpaceBaseAddress(),
  52. vm.GetAddressSpaceEndAddress());
  53. }
  54. bool IsInsideNewMapRegion(const VMManager& vm, VAddr address, u64 size) {
  55. return IsInsideAddressRange(address, size, vm.GetNewMapRegionBaseAddress(),
  56. vm.GetNewMapRegionEndAddress());
  57. }
  58. // 8 GiB
  59. constexpr u64 MAIN_MEMORY_SIZE = 0x200000000;
  60. // Helper function that performs the common sanity checks for svcMapMemory
  61. // and svcUnmapMemory. This is doable, as both functions perform their sanitizing
  62. // in the same order.
  63. ResultCode MapUnmapMemorySanityChecks(const VMManager& vm_manager, VAddr dst_addr, VAddr src_addr,
  64. u64 size) {
  65. if (!Common::Is4KBAligned(dst_addr)) {
  66. LOG_ERROR(Kernel_SVC, "Destination address is not aligned to 4KB, 0x{:016X}", dst_addr);
  67. return ERR_INVALID_ADDRESS;
  68. }
  69. if (!Common::Is4KBAligned(src_addr)) {
  70. LOG_ERROR(Kernel_SVC, "Source address is not aligned to 4KB, 0x{:016X}", src_addr);
  71. return ERR_INVALID_SIZE;
  72. }
  73. if (size == 0) {
  74. LOG_ERROR(Kernel_SVC, "Size is 0");
  75. return ERR_INVALID_SIZE;
  76. }
  77. if (!Common::Is4KBAligned(size)) {
  78. LOG_ERROR(Kernel_SVC, "Size is not aligned to 4KB, 0x{:016X}", size);
  79. return ERR_INVALID_SIZE;
  80. }
  81. if (!IsValidAddressRange(dst_addr, size)) {
  82. LOG_ERROR(Kernel_SVC,
  83. "Destination is not a valid address range, addr=0x{:016X}, size=0x{:016X}",
  84. dst_addr, size);
  85. return ERR_INVALID_ADDRESS_STATE;
  86. }
  87. if (!IsValidAddressRange(src_addr, size)) {
  88. LOG_ERROR(Kernel_SVC, "Source is not a valid address range, addr=0x{:016X}, size=0x{:016X}",
  89. src_addr, size);
  90. return ERR_INVALID_ADDRESS_STATE;
  91. }
  92. if (!IsInsideAddressSpace(vm_manager, src_addr, size)) {
  93. LOG_ERROR(Kernel_SVC,
  94. "Source is not within the address space, addr=0x{:016X}, size=0x{:016X}",
  95. src_addr, size);
  96. return ERR_INVALID_ADDRESS_STATE;
  97. }
  98. if (!IsInsideNewMapRegion(vm_manager, dst_addr, size)) {
  99. LOG_ERROR(Kernel_SVC,
  100. "Destination is not within the new map region, addr=0x{:016X}, size=0x{:016X}",
  101. dst_addr, size);
  102. return ERR_INVALID_MEMORY_RANGE;
  103. }
  104. const VAddr dst_end_address = dst_addr + size;
  105. if (dst_end_address > vm_manager.GetHeapRegionBaseAddress() &&
  106. vm_manager.GetHeapRegionEndAddress() > dst_addr) {
  107. LOG_ERROR(Kernel_SVC,
  108. "Destination does not fit within the heap region, addr=0x{:016X}, "
  109. "size=0x{:016X}, end_addr=0x{:016X}",
  110. dst_addr, size, dst_end_address);
  111. return ERR_INVALID_MEMORY_RANGE;
  112. }
  113. if (dst_end_address > vm_manager.GetMapRegionBaseAddress() &&
  114. vm_manager.GetMapRegionEndAddress() > dst_addr) {
  115. LOG_ERROR(Kernel_SVC,
  116. "Destination does not fit within the map region, addr=0x{:016X}, "
  117. "size=0x{:016X}, end_addr=0x{:016X}",
  118. dst_addr, size, dst_end_address);
  119. return ERR_INVALID_MEMORY_RANGE;
  120. }
  121. return RESULT_SUCCESS;
  122. }
  123. enum class ResourceLimitValueType {
  124. CurrentValue,
  125. LimitValue,
  126. };
  127. ResultVal<s64> RetrieveResourceLimitValue(Handle resource_limit, u32 resource_type,
  128. ResourceLimitValueType value_type) {
  129. const auto type = static_cast<ResourceType>(resource_type);
  130. if (!IsValidResourceType(type)) {
  131. LOG_ERROR(Kernel_SVC, "Invalid resource limit type: '{}'", resource_type);
  132. return ERR_INVALID_ENUM_VALUE;
  133. }
  134. const auto& kernel = Core::System::GetInstance().Kernel();
  135. const auto* const current_process = kernel.CurrentProcess();
  136. ASSERT(current_process != nullptr);
  137. const auto resource_limit_object =
  138. current_process->GetHandleTable().Get<ResourceLimit>(resource_limit);
  139. if (!resource_limit_object) {
  140. LOG_ERROR(Kernel_SVC, "Handle to non-existent resource limit instance used. Handle={:08X}",
  141. resource_limit);
  142. return ERR_INVALID_HANDLE;
  143. }
  144. if (value_type == ResourceLimitValueType::CurrentValue) {
  145. return MakeResult(resource_limit_object->GetCurrentResourceValue(type));
  146. }
  147. return MakeResult(resource_limit_object->GetMaxResourceValue(type));
  148. }
  149. } // Anonymous namespace
  150. /// Set the process heap to a given Size. It can both extend and shrink the heap.
  151. static ResultCode SetHeapSize(VAddr* heap_addr, u64 heap_size) {
  152. LOG_TRACE(Kernel_SVC, "called, heap_size=0x{:X}", heap_size);
  153. // Size must be a multiple of 0x200000 (2MB) and be equal to or less than 8GB.
  154. if ((heap_size % 0x200000) != 0) {
  155. LOG_ERROR(Kernel_SVC, "The heap size is not a multiple of 2MB, heap_size=0x{:016X}",
  156. heap_size);
  157. return ERR_INVALID_SIZE;
  158. }
  159. if (heap_size >= 0x200000000) {
  160. LOG_ERROR(Kernel_SVC, "The heap size is not less than 8GB, heap_size=0x{:016X}", heap_size);
  161. return ERR_INVALID_SIZE;
  162. }
  163. auto& process = *Core::CurrentProcess();
  164. const VAddr heap_base = process.VMManager().GetHeapRegionBaseAddress();
  165. CASCADE_RESULT(*heap_addr,
  166. process.HeapAllocate(heap_base, heap_size, VMAPermission::ReadWrite));
  167. return RESULT_SUCCESS;
  168. }
  169. static ResultCode SetMemoryPermission(VAddr addr, u64 size, u32 prot) {
  170. LOG_TRACE(Kernel_SVC, "called, addr=0x{:X}, size=0x{:X}, prot=0x{:X}", addr, size, prot);
  171. if (!Common::Is4KBAligned(addr)) {
  172. LOG_ERROR(Kernel_SVC, "Address is not aligned to 4KB, addr=0x{:016X}", addr);
  173. return ERR_INVALID_ADDRESS;
  174. }
  175. if (size == 0) {
  176. LOG_ERROR(Kernel_SVC, "Size is 0");
  177. return ERR_INVALID_SIZE;
  178. }
  179. if (!Common::Is4KBAligned(size)) {
  180. LOG_ERROR(Kernel_SVC, "Size is not aligned to 4KB, size=0x{:016X}", size);
  181. return ERR_INVALID_SIZE;
  182. }
  183. if (!IsValidAddressRange(addr, size)) {
  184. LOG_ERROR(Kernel_SVC, "Region is not a valid address range, addr=0x{:016X}, size=0x{:016X}",
  185. addr, size);
  186. return ERR_INVALID_ADDRESS_STATE;
  187. }
  188. const auto permission = static_cast<MemoryPermission>(prot);
  189. if (permission != MemoryPermission::None && permission != MemoryPermission::Read &&
  190. permission != MemoryPermission::ReadWrite) {
  191. LOG_ERROR(Kernel_SVC, "Invalid memory permission specified, Got memory permission=0x{:08X}",
  192. static_cast<u32>(permission));
  193. return ERR_INVALID_MEMORY_PERMISSIONS;
  194. }
  195. auto* const current_process = Core::CurrentProcess();
  196. auto& vm_manager = current_process->VMManager();
  197. if (!IsInsideAddressSpace(vm_manager, addr, size)) {
  198. LOG_ERROR(Kernel_SVC,
  199. "Source is not within the address space, addr=0x{:016X}, size=0x{:016X}", addr,
  200. size);
  201. return ERR_INVALID_ADDRESS_STATE;
  202. }
  203. const VMManager::VMAHandle iter = vm_manager.FindVMA(addr);
  204. if (!vm_manager.IsValidHandle(iter)) {
  205. LOG_ERROR(Kernel_SVC, "Unable to find VMA for address=0x{:016X}", addr);
  206. return ERR_INVALID_ADDRESS_STATE;
  207. }
  208. LOG_WARNING(Kernel_SVC, "Uniformity check on protected memory is not implemented.");
  209. // TODO: Performs a uniformity check to make sure only protected memory is changed (it doesn't
  210. // make sense to allow changing permissions on kernel memory itself, etc).
  211. const auto converted_permissions = SharedMemory::ConvertPermissions(permission);
  212. return vm_manager.ReprotectRange(addr, size, converted_permissions);
  213. }
  214. static ResultCode SetMemoryAttribute(VAddr addr, u64 size, u32 state0, u32 state1) {
  215. LOG_WARNING(Kernel_SVC,
  216. "(STUBBED) called, addr=0x{:X}, size=0x{:X}, state0=0x{:X}, state1=0x{:X}", addr,
  217. size, state0, state1);
  218. return RESULT_SUCCESS;
  219. }
  220. /// Maps a memory range into a different range.
  221. static ResultCode MapMemory(VAddr dst_addr, VAddr src_addr, u64 size) {
  222. LOG_TRACE(Kernel_SVC, "called, dst_addr=0x{:X}, src_addr=0x{:X}, size=0x{:X}", dst_addr,
  223. src_addr, size);
  224. auto* const current_process = Core::CurrentProcess();
  225. const auto& vm_manager = current_process->VMManager();
  226. const auto result = MapUnmapMemorySanityChecks(vm_manager, dst_addr, src_addr, size);
  227. if (result != RESULT_SUCCESS) {
  228. return result;
  229. }
  230. return current_process->MirrorMemory(dst_addr, src_addr, size);
  231. }
  232. /// Unmaps a region that was previously mapped with svcMapMemory
  233. static ResultCode UnmapMemory(VAddr dst_addr, VAddr src_addr, u64 size) {
  234. LOG_TRACE(Kernel_SVC, "called, dst_addr=0x{:X}, src_addr=0x{:X}, size=0x{:X}", dst_addr,
  235. src_addr, size);
  236. auto* const current_process = Core::CurrentProcess();
  237. const auto& vm_manager = current_process->VMManager();
  238. const auto result = MapUnmapMemorySanityChecks(vm_manager, dst_addr, src_addr, size);
  239. if (result != RESULT_SUCCESS) {
  240. return result;
  241. }
  242. return current_process->UnmapMemory(dst_addr, src_addr, size);
  243. }
  244. /// Connect to an OS service given the port name, returns the handle to the port to out
  245. static ResultCode ConnectToNamedPort(Handle* out_handle, VAddr port_name_address) {
  246. if (!Memory::IsValidVirtualAddress(port_name_address)) {
  247. LOG_ERROR(Kernel_SVC,
  248. "Port Name Address is not a valid virtual address, port_name_address=0x{:016X}",
  249. port_name_address);
  250. return ERR_NOT_FOUND;
  251. }
  252. static constexpr std::size_t PortNameMaxLength = 11;
  253. // Read 1 char beyond the max allowed port name to detect names that are too long.
  254. std::string port_name = Memory::ReadCString(port_name_address, PortNameMaxLength + 1);
  255. if (port_name.size() > PortNameMaxLength) {
  256. LOG_ERROR(Kernel_SVC, "Port name is too long, expected {} but got {}", PortNameMaxLength,
  257. port_name.size());
  258. return ERR_OUT_OF_RANGE;
  259. }
  260. LOG_TRACE(Kernel_SVC, "called port_name={}", port_name);
  261. auto& kernel = Core::System::GetInstance().Kernel();
  262. auto it = kernel.FindNamedPort(port_name);
  263. if (!kernel.IsValidNamedPort(it)) {
  264. LOG_WARNING(Kernel_SVC, "tried to connect to unknown port: {}", port_name);
  265. return ERR_NOT_FOUND;
  266. }
  267. auto client_port = it->second;
  268. SharedPtr<ClientSession> client_session;
  269. CASCADE_RESULT(client_session, client_port->Connect());
  270. // Return the client session
  271. auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  272. CASCADE_RESULT(*out_handle, handle_table.Create(client_session));
  273. return RESULT_SUCCESS;
  274. }
  275. /// Makes a blocking IPC call to an OS service.
  276. static ResultCode SendSyncRequest(Handle handle) {
  277. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  278. SharedPtr<ClientSession> session = handle_table.Get<ClientSession>(handle);
  279. if (!session) {
  280. LOG_ERROR(Kernel_SVC, "called with invalid handle=0x{:08X}", handle);
  281. return ERR_INVALID_HANDLE;
  282. }
  283. LOG_TRACE(Kernel_SVC, "called handle=0x{:08X}({})", handle, session->GetName());
  284. Core::System::GetInstance().PrepareReschedule();
  285. // TODO(Subv): svcSendSyncRequest should put the caller thread to sleep while the server
  286. // responds and cause a reschedule.
  287. return session->SendSyncRequest(GetCurrentThread());
  288. }
  289. /// Get the ID for the specified thread.
  290. static ResultCode GetThreadId(u32* thread_id, Handle thread_handle) {
  291. LOG_TRACE(Kernel_SVC, "called thread=0x{:08X}", thread_handle);
  292. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  293. const SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  294. if (!thread) {
  295. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", thread_handle);
  296. return ERR_INVALID_HANDLE;
  297. }
  298. *thread_id = thread->GetThreadID();
  299. return RESULT_SUCCESS;
  300. }
  301. /// Get the ID of the specified process
  302. static ResultCode GetProcessId(u32* process_id, Handle process_handle) {
  303. LOG_TRACE(Kernel_SVC, "called process=0x{:08X}", process_handle);
  304. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  305. const SharedPtr<Process> process = handle_table.Get<Process>(process_handle);
  306. if (!process) {
  307. LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}",
  308. process_handle);
  309. return ERR_INVALID_HANDLE;
  310. }
  311. *process_id = process->GetProcessID();
  312. return RESULT_SUCCESS;
  313. }
  314. /// Default thread wakeup callback for WaitSynchronization
  315. static bool DefaultThreadWakeupCallback(ThreadWakeupReason reason, SharedPtr<Thread> thread,
  316. SharedPtr<WaitObject> object, std::size_t index) {
  317. ASSERT(thread->GetStatus() == ThreadStatus::WaitSynchAny);
  318. if (reason == ThreadWakeupReason::Timeout) {
  319. thread->SetWaitSynchronizationResult(RESULT_TIMEOUT);
  320. return true;
  321. }
  322. ASSERT(reason == ThreadWakeupReason::Signal);
  323. thread->SetWaitSynchronizationResult(RESULT_SUCCESS);
  324. thread->SetWaitSynchronizationOutput(static_cast<u32>(index));
  325. return true;
  326. };
  327. /// Wait for the given handles to synchronize, timeout after the specified nanoseconds
  328. static ResultCode WaitSynchronization(Handle* index, VAddr handles_address, u64 handle_count,
  329. s64 nano_seconds) {
  330. LOG_TRACE(Kernel_SVC, "called handles_address=0x{:X}, handle_count={}, nano_seconds={}",
  331. handles_address, handle_count, nano_seconds);
  332. if (!Memory::IsValidVirtualAddress(handles_address)) {
  333. LOG_ERROR(Kernel_SVC,
  334. "Handle address is not a valid virtual address, handle_address=0x{:016X}",
  335. handles_address);
  336. return ERR_INVALID_POINTER;
  337. }
  338. static constexpr u64 MaxHandles = 0x40;
  339. if (handle_count > MaxHandles) {
  340. LOG_ERROR(Kernel_SVC, "Handle count specified is too large, expected {} but got {}",
  341. MaxHandles, handle_count);
  342. return ERR_OUT_OF_RANGE;
  343. }
  344. auto* const thread = GetCurrentThread();
  345. using ObjectPtr = Thread::ThreadWaitObjects::value_type;
  346. Thread::ThreadWaitObjects objects(handle_count);
  347. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  348. for (u64 i = 0; i < handle_count; ++i) {
  349. const Handle handle = Memory::Read32(handles_address + i * sizeof(Handle));
  350. const auto object = handle_table.Get<WaitObject>(handle);
  351. if (object == nullptr) {
  352. LOG_ERROR(Kernel_SVC, "Object is a nullptr");
  353. return ERR_INVALID_HANDLE;
  354. }
  355. objects[i] = object;
  356. }
  357. // Find the first object that is acquirable in the provided list of objects
  358. auto itr = std::find_if(objects.begin(), objects.end(), [thread](const ObjectPtr& object) {
  359. return !object->ShouldWait(thread);
  360. });
  361. if (itr != objects.end()) {
  362. // We found a ready object, acquire it and set the result value
  363. WaitObject* object = itr->get();
  364. object->Acquire(thread);
  365. *index = static_cast<s32>(std::distance(objects.begin(), itr));
  366. return RESULT_SUCCESS;
  367. }
  368. // No objects were ready to be acquired, prepare to suspend the thread.
  369. // If a timeout value of 0 was provided, just return the Timeout error code instead of
  370. // suspending the thread.
  371. if (nano_seconds == 0) {
  372. return RESULT_TIMEOUT;
  373. }
  374. for (auto& object : objects) {
  375. object->AddWaitingThread(thread);
  376. }
  377. thread->SetWaitObjects(std::move(objects));
  378. thread->SetStatus(ThreadStatus::WaitSynchAny);
  379. // Create an event to wake the thread up after the specified nanosecond delay has passed
  380. thread->WakeAfterDelay(nano_seconds);
  381. thread->SetWakeupCallback(DefaultThreadWakeupCallback);
  382. Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule();
  383. return RESULT_TIMEOUT;
  384. }
  385. /// Resumes a thread waiting on WaitSynchronization
  386. static ResultCode CancelSynchronization(Handle thread_handle) {
  387. LOG_TRACE(Kernel_SVC, "called thread=0x{:X}", thread_handle);
  388. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  389. const SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  390. if (!thread) {
  391. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, thread_handle=0x{:08X}",
  392. thread_handle);
  393. return ERR_INVALID_HANDLE;
  394. }
  395. ASSERT(thread->GetStatus() == ThreadStatus::WaitSynchAny);
  396. thread->SetWaitSynchronizationResult(ERR_SYNCHRONIZATION_CANCELED);
  397. thread->ResumeFromWait();
  398. return RESULT_SUCCESS;
  399. }
  400. /// Attempts to locks a mutex, creating it if it does not already exist
  401. static ResultCode ArbitrateLock(Handle holding_thread_handle, VAddr mutex_addr,
  402. Handle requesting_thread_handle) {
  403. LOG_TRACE(Kernel_SVC,
  404. "called holding_thread_handle=0x{:08X}, mutex_addr=0x{:X}, "
  405. "requesting_current_thread_handle=0x{:08X}",
  406. holding_thread_handle, mutex_addr, requesting_thread_handle);
  407. if (Memory::IsKernelVirtualAddress(mutex_addr)) {
  408. LOG_ERROR(Kernel_SVC, "Mutex Address is a kernel virtual address, mutex_addr={:016X}",
  409. mutex_addr);
  410. return ERR_INVALID_ADDRESS_STATE;
  411. }
  412. if (!Common::IsWordAligned(mutex_addr)) {
  413. LOG_ERROR(Kernel_SVC, "Mutex Address is not word aligned, mutex_addr={:016X}", mutex_addr);
  414. return ERR_INVALID_ADDRESS;
  415. }
  416. auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  417. return Mutex::TryAcquire(handle_table, mutex_addr, holding_thread_handle,
  418. requesting_thread_handle);
  419. }
  420. /// Unlock a mutex
  421. static ResultCode ArbitrateUnlock(VAddr mutex_addr) {
  422. LOG_TRACE(Kernel_SVC, "called mutex_addr=0x{:X}", mutex_addr);
  423. if (Memory::IsKernelVirtualAddress(mutex_addr)) {
  424. LOG_ERROR(Kernel_SVC, "Mutex Address is a kernel virtual address, mutex_addr={:016X}",
  425. mutex_addr);
  426. return ERR_INVALID_ADDRESS_STATE;
  427. }
  428. if (!Common::IsWordAligned(mutex_addr)) {
  429. LOG_ERROR(Kernel_SVC, "Mutex Address is not word aligned, mutex_addr={:016X}", mutex_addr);
  430. return ERR_INVALID_ADDRESS;
  431. }
  432. return Mutex::Release(mutex_addr);
  433. }
  434. enum class BreakType : u32 {
  435. Panic = 0,
  436. AssertionFailed = 1,
  437. PreNROLoad = 3,
  438. PostNROLoad = 4,
  439. PreNROUnload = 5,
  440. PostNROUnload = 6,
  441. };
  442. struct BreakReason {
  443. union {
  444. u32 raw;
  445. BitField<0, 30, BreakType> break_type;
  446. BitField<31, 1, u32> signal_debugger;
  447. };
  448. };
  449. /// Break program execution
  450. static void Break(u32 reason, u64 info1, u64 info2) {
  451. BreakReason break_reason{reason};
  452. bool has_dumped_buffer{};
  453. const auto handle_debug_buffer = [&](VAddr addr, u64 sz) {
  454. if (sz == 0 || addr == 0 || has_dumped_buffer) {
  455. return;
  456. }
  457. // This typically is an error code so we're going to assume this is the case
  458. if (sz == sizeof(u32)) {
  459. LOG_CRITICAL(Debug_Emulated, "debug_buffer_err_code={:X}", Memory::Read32(addr));
  460. } else {
  461. // We don't know what's in here so we'll hexdump it
  462. std::vector<u8> debug_buffer(sz);
  463. Memory::ReadBlock(addr, debug_buffer.data(), sz);
  464. std::string hexdump;
  465. for (std::size_t i = 0; i < debug_buffer.size(); i++) {
  466. hexdump += fmt::format("{:02X} ", debug_buffer[i]);
  467. if (i != 0 && i % 16 == 0) {
  468. hexdump += '\n';
  469. }
  470. }
  471. LOG_CRITICAL(Debug_Emulated, "debug_buffer=\n{}", hexdump);
  472. }
  473. has_dumped_buffer = true;
  474. };
  475. switch (break_reason.break_type) {
  476. case BreakType::Panic:
  477. LOG_CRITICAL(Debug_Emulated, "Signalling debugger, PANIC! info1=0x{:016X}, info2=0x{:016X}",
  478. info1, info2);
  479. handle_debug_buffer(info1, info2);
  480. break;
  481. case BreakType::AssertionFailed:
  482. LOG_CRITICAL(Debug_Emulated,
  483. "Signalling debugger, Assertion failed! info1=0x{:016X}, info2=0x{:016X}",
  484. info1, info2);
  485. handle_debug_buffer(info1, info2);
  486. break;
  487. case BreakType::PreNROLoad:
  488. LOG_WARNING(
  489. Debug_Emulated,
  490. "Signalling debugger, Attempting to load an NRO at 0x{:016X} with size 0x{:016X}",
  491. info1, info2);
  492. break;
  493. case BreakType::PostNROLoad:
  494. LOG_WARNING(Debug_Emulated,
  495. "Signalling debugger, Loaded an NRO at 0x{:016X} with size 0x{:016X}", info1,
  496. info2);
  497. break;
  498. case BreakType::PreNROUnload:
  499. LOG_WARNING(
  500. Debug_Emulated,
  501. "Signalling debugger, Attempting to unload an NRO at 0x{:016X} with size 0x{:016X}",
  502. info1, info2);
  503. break;
  504. case BreakType::PostNROUnload:
  505. LOG_WARNING(Debug_Emulated,
  506. "Signalling debugger, Unloaded an NRO at 0x{:016X} with size 0x{:016X}", info1,
  507. info2);
  508. break;
  509. default:
  510. LOG_WARNING(
  511. Debug_Emulated,
  512. "Signalling debugger, Unknown break reason {}, info1=0x{:016X}, info2=0x{:016X}",
  513. static_cast<u32>(break_reason.break_type.Value()), info1, info2);
  514. handle_debug_buffer(info1, info2);
  515. break;
  516. }
  517. if (!break_reason.signal_debugger) {
  518. LOG_CRITICAL(
  519. Debug_Emulated,
  520. "Emulated program broke execution! reason=0x{:016X}, info1=0x{:016X}, info2=0x{:016X}",
  521. reason, info1, info2);
  522. handle_debug_buffer(info1, info2);
  523. ASSERT(false);
  524. Core::CurrentProcess()->PrepareForTermination();
  525. // Kill the current thread
  526. GetCurrentThread()->Stop();
  527. Core::System::GetInstance().PrepareReschedule();
  528. }
  529. }
  530. /// Used to output a message on a debug hardware unit - does nothing on a retail unit
  531. static void OutputDebugString(VAddr address, u64 len) {
  532. if (len == 0) {
  533. return;
  534. }
  535. std::string str(len, '\0');
  536. Memory::ReadBlock(address, str.data(), str.size());
  537. LOG_DEBUG(Debug_Emulated, "{}", str);
  538. }
  539. /// Gets system/memory information for the current process
  540. static ResultCode GetInfo(u64* result, u64 info_id, u64 handle, u64 info_sub_id) {
  541. LOG_TRACE(Kernel_SVC, "called info_id=0x{:X}, info_sub_id=0x{:X}, handle=0x{:08X}", info_id,
  542. info_sub_id, handle);
  543. enum class GetInfoType : u64 {
  544. // 1.0.0+
  545. AllowedCpuIdBitmask = 0,
  546. AllowedThreadPrioBitmask = 1,
  547. MapRegionBaseAddr = 2,
  548. MapRegionSize = 3,
  549. HeapRegionBaseAddr = 4,
  550. HeapRegionSize = 5,
  551. TotalMemoryUsage = 6,
  552. TotalHeapUsage = 7,
  553. IsCurrentProcessBeingDebugged = 8,
  554. RegisterResourceLimit = 9,
  555. IdleTickCount = 10,
  556. RandomEntropy = 11,
  557. PerformanceCounter = 0xF0000002,
  558. // 2.0.0+
  559. ASLRRegionBaseAddr = 12,
  560. ASLRRegionSize = 13,
  561. NewMapRegionBaseAddr = 14,
  562. NewMapRegionSize = 15,
  563. // 3.0.0+
  564. IsVirtualAddressMemoryEnabled = 16,
  565. PersonalMmHeapUsage = 17,
  566. TitleId = 18,
  567. // 4.0.0+
  568. PrivilegedProcessId = 19,
  569. // 5.0.0+
  570. UserExceptionContextAddr = 20,
  571. ThreadTickCount = 0xF0000002,
  572. };
  573. const auto info_id_type = static_cast<GetInfoType>(info_id);
  574. switch (info_id_type) {
  575. case GetInfoType::AllowedCpuIdBitmask:
  576. case GetInfoType::AllowedThreadPrioBitmask:
  577. case GetInfoType::MapRegionBaseAddr:
  578. case GetInfoType::MapRegionSize:
  579. case GetInfoType::HeapRegionBaseAddr:
  580. case GetInfoType::HeapRegionSize:
  581. case GetInfoType::ASLRRegionBaseAddr:
  582. case GetInfoType::ASLRRegionSize:
  583. case GetInfoType::NewMapRegionBaseAddr:
  584. case GetInfoType::NewMapRegionSize:
  585. case GetInfoType::TotalMemoryUsage:
  586. case GetInfoType::TotalHeapUsage:
  587. case GetInfoType::IsVirtualAddressMemoryEnabled:
  588. case GetInfoType::PersonalMmHeapUsage:
  589. case GetInfoType::TitleId:
  590. case GetInfoType::UserExceptionContextAddr: {
  591. if (info_sub_id != 0) {
  592. return ERR_INVALID_ENUM_VALUE;
  593. }
  594. const auto& current_process_handle_table = Core::CurrentProcess()->GetHandleTable();
  595. const auto process = current_process_handle_table.Get<Process>(static_cast<Handle>(handle));
  596. if (!process) {
  597. return ERR_INVALID_HANDLE;
  598. }
  599. switch (info_id_type) {
  600. case GetInfoType::AllowedCpuIdBitmask:
  601. *result = process->GetAllowedProcessorMask();
  602. return RESULT_SUCCESS;
  603. case GetInfoType::AllowedThreadPrioBitmask:
  604. *result = process->GetAllowedThreadPriorityMask();
  605. return RESULT_SUCCESS;
  606. case GetInfoType::MapRegionBaseAddr:
  607. *result = process->VMManager().GetMapRegionBaseAddress();
  608. return RESULT_SUCCESS;
  609. case GetInfoType::MapRegionSize:
  610. *result = process->VMManager().GetMapRegionSize();
  611. return RESULT_SUCCESS;
  612. case GetInfoType::HeapRegionBaseAddr:
  613. *result = process->VMManager().GetHeapRegionBaseAddress();
  614. return RESULT_SUCCESS;
  615. case GetInfoType::HeapRegionSize:
  616. *result = process->VMManager().GetHeapRegionSize();
  617. return RESULT_SUCCESS;
  618. case GetInfoType::ASLRRegionBaseAddr:
  619. *result = process->VMManager().GetASLRRegionBaseAddress();
  620. return RESULT_SUCCESS;
  621. case GetInfoType::ASLRRegionSize:
  622. *result = process->VMManager().GetASLRRegionSize();
  623. return RESULT_SUCCESS;
  624. case GetInfoType::NewMapRegionBaseAddr:
  625. *result = process->VMManager().GetNewMapRegionBaseAddress();
  626. return RESULT_SUCCESS;
  627. case GetInfoType::NewMapRegionSize:
  628. *result = process->VMManager().GetNewMapRegionSize();
  629. return RESULT_SUCCESS;
  630. case GetInfoType::TotalMemoryUsage:
  631. *result = process->VMManager().GetTotalMemoryUsage();
  632. return RESULT_SUCCESS;
  633. case GetInfoType::TotalHeapUsage:
  634. *result = process->VMManager().GetTotalHeapUsage();
  635. return RESULT_SUCCESS;
  636. case GetInfoType::IsVirtualAddressMemoryEnabled:
  637. *result = process->IsVirtualMemoryEnabled();
  638. return RESULT_SUCCESS;
  639. case GetInfoType::TitleId:
  640. *result = process->GetTitleID();
  641. return RESULT_SUCCESS;
  642. case GetInfoType::UserExceptionContextAddr:
  643. LOG_WARNING(Kernel_SVC,
  644. "(STUBBED) Attempted to query user exception context address, returned 0");
  645. *result = 0;
  646. return RESULT_SUCCESS;
  647. default:
  648. break;
  649. }
  650. LOG_WARNING(Kernel_SVC, "(STUBBED) 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. return ERR_INVALID_HANDLE;
  659. }
  660. if (info_sub_id != 0) {
  661. return ERR_INVALID_COMBINATION;
  662. }
  663. Process* const current_process = Core::CurrentProcess();
  664. HandleTable& handle_table = current_process->GetHandleTable();
  665. const auto resource_limit = current_process->GetResourceLimit();
  666. if (!resource_limit) {
  667. *result = KernelHandle::InvalidHandle;
  668. // Yes, the kernel considers this a successful operation.
  669. return RESULT_SUCCESS;
  670. }
  671. const auto table_result = handle_table.Create(resource_limit);
  672. if (table_result.Failed()) {
  673. return table_result.Code();
  674. }
  675. *result = *table_result;
  676. return RESULT_SUCCESS;
  677. }
  678. case GetInfoType::RandomEntropy:
  679. if (handle != 0) {
  680. LOG_ERROR(Kernel_SVC, "Process Handle is non zero, expected 0 result but got {:016X}",
  681. handle);
  682. return ERR_INVALID_HANDLE;
  683. }
  684. if (info_sub_id >= Process::RANDOM_ENTROPY_SIZE) {
  685. LOG_ERROR(Kernel_SVC, "Entropy size is out of range, expected {} but got {}",
  686. Process::RANDOM_ENTROPY_SIZE, info_sub_id);
  687. return ERR_INVALID_COMBINATION;
  688. }
  689. *result = Core::CurrentProcess()->GetRandomEntropy(info_sub_id);
  690. return RESULT_SUCCESS;
  691. case GetInfoType::PrivilegedProcessId:
  692. LOG_WARNING(Kernel_SVC,
  693. "(STUBBED) Attempted to query privileged process id bounds, returned 0");
  694. *result = 0;
  695. return RESULT_SUCCESS;
  696. case GetInfoType::ThreadTickCount: {
  697. constexpr u64 num_cpus = 4;
  698. if (info_sub_id != 0xFFFFFFFFFFFFFFFF && info_sub_id >= num_cpus) {
  699. LOG_ERROR(Kernel_SVC, "Core count is out of range, expected {} but got {}", num_cpus,
  700. info_sub_id);
  701. return ERR_INVALID_COMBINATION;
  702. }
  703. const auto thread =
  704. Core::CurrentProcess()->GetHandleTable().Get<Thread>(static_cast<Handle>(handle));
  705. if (!thread) {
  706. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}",
  707. static_cast<Handle>(handle));
  708. return ERR_INVALID_HANDLE;
  709. }
  710. const auto& system = Core::System::GetInstance();
  711. const auto& scheduler = system.CurrentScheduler();
  712. const auto* const current_thread = scheduler.GetCurrentThread();
  713. const bool same_thread = current_thread == thread;
  714. const u64 prev_ctx_ticks = scheduler.GetLastContextSwitchTicks();
  715. u64 out_ticks = 0;
  716. if (same_thread && info_sub_id == 0xFFFFFFFFFFFFFFFF) {
  717. const u64 thread_ticks = current_thread->GetTotalCPUTimeTicks();
  718. out_ticks = thread_ticks + (CoreTiming::GetTicks() - prev_ctx_ticks);
  719. } else if (same_thread && info_sub_id == system.CurrentCoreIndex()) {
  720. out_ticks = CoreTiming::GetTicks() - prev_ctx_ticks;
  721. }
  722. *result = out_ticks;
  723. return RESULT_SUCCESS;
  724. }
  725. default:
  726. LOG_WARNING(Kernel_SVC, "(STUBBED) Unimplemented svcGetInfo id=0x{:016X}", info_id);
  727. return ERR_INVALID_ENUM_VALUE;
  728. }
  729. }
  730. /// Sets the thread activity
  731. static ResultCode SetThreadActivity(Handle handle, u32 unknown) {
  732. LOG_WARNING(Kernel_SVC, "(STUBBED) called, handle=0x{:08X}, unknown=0x{:08X}", handle, unknown);
  733. return RESULT_SUCCESS;
  734. }
  735. /// Gets the thread context
  736. static ResultCode GetThreadContext(VAddr thread_context, Handle handle) {
  737. LOG_DEBUG(Kernel_SVC, "called, context=0x{:08X}, thread=0x{:X}", thread_context, handle);
  738. const auto* current_process = Core::CurrentProcess();
  739. const SharedPtr<Thread> thread = current_process->GetHandleTable().Get<Thread>(handle);
  740. if (!thread) {
  741. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", handle);
  742. return ERR_INVALID_HANDLE;
  743. }
  744. if (thread->GetOwnerProcess() != current_process) {
  745. LOG_ERROR(Kernel_SVC,
  746. "The current process does not own the current thread, thread_handle={:08X} "
  747. "thread_pid={}, "
  748. "current_process_pid={}",
  749. handle, thread->GetOwnerProcess()->GetProcessID(),
  750. current_process->GetProcessID());
  751. return ERR_INVALID_HANDLE;
  752. }
  753. if (thread == GetCurrentThread()) {
  754. LOG_ERROR(Kernel_SVC, "The thread handle specified is the current running thread");
  755. return ERR_ALREADY_REGISTERED;
  756. }
  757. Core::ARM_Interface::ThreadContext ctx = thread->GetContext();
  758. // Mask away mode bits, interrupt bits, IL bit, and other reserved bits.
  759. ctx.pstate &= 0xFF0FFE20;
  760. // If 64-bit, we can just write the context registers directly and we're good.
  761. // However, if 32-bit, we have to ensure some registers are zeroed out.
  762. if (!current_process->Is64BitProcess()) {
  763. std::fill(ctx.cpu_registers.begin() + 15, ctx.cpu_registers.end(), 0);
  764. std::fill(ctx.vector_registers.begin() + 16, ctx.vector_registers.end(), u128{});
  765. }
  766. Memory::WriteBlock(thread_context, &ctx, sizeof(ctx));
  767. return RESULT_SUCCESS;
  768. }
  769. /// Gets the priority for the specified thread
  770. static ResultCode GetThreadPriority(u32* priority, Handle handle) {
  771. LOG_TRACE(Kernel_SVC, "called");
  772. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  773. const SharedPtr<Thread> thread = handle_table.Get<Thread>(handle);
  774. if (!thread) {
  775. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", handle);
  776. return ERR_INVALID_HANDLE;
  777. }
  778. *priority = thread->GetPriority();
  779. return RESULT_SUCCESS;
  780. }
  781. /// Sets the priority for the specified thread
  782. static ResultCode SetThreadPriority(Handle handle, u32 priority) {
  783. LOG_TRACE(Kernel_SVC, "called");
  784. if (priority > THREADPRIO_LOWEST) {
  785. LOG_ERROR(
  786. Kernel_SVC,
  787. "An invalid priority was specified, expected {} but got {} for thread_handle={:08X}",
  788. THREADPRIO_LOWEST, priority, handle);
  789. return ERR_INVALID_THREAD_PRIORITY;
  790. }
  791. const auto* const current_process = Core::CurrentProcess();
  792. SharedPtr<Thread> thread = current_process->GetHandleTable().Get<Thread>(handle);
  793. if (!thread) {
  794. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, handle=0x{:08X}", handle);
  795. return ERR_INVALID_HANDLE;
  796. }
  797. thread->SetPriority(priority);
  798. Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule();
  799. return RESULT_SUCCESS;
  800. }
  801. /// Get which CPU core is executing the current thread
  802. static u32 GetCurrentProcessorNumber() {
  803. LOG_TRACE(Kernel_SVC, "called");
  804. return GetCurrentThread()->GetProcessorID();
  805. }
  806. static ResultCode MapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 size,
  807. u32 permissions) {
  808. LOG_TRACE(Kernel_SVC,
  809. "called, shared_memory_handle=0x{:X}, addr=0x{:X}, size=0x{:X}, permissions=0x{:08X}",
  810. shared_memory_handle, addr, size, permissions);
  811. if (!Common::Is4KBAligned(addr)) {
  812. LOG_ERROR(Kernel_SVC, "Address is not aligned to 4KB, addr=0x{:016X}", addr);
  813. return ERR_INVALID_ADDRESS;
  814. }
  815. if (size == 0) {
  816. LOG_ERROR(Kernel_SVC, "Size is 0");
  817. return ERR_INVALID_SIZE;
  818. }
  819. if (!Common::Is4KBAligned(size)) {
  820. LOG_ERROR(Kernel_SVC, "Size is not aligned to 4KB, size=0x{:016X}", size);
  821. return ERR_INVALID_SIZE;
  822. }
  823. if (!IsValidAddressRange(addr, size)) {
  824. LOG_ERROR(Kernel_SVC, "Region is not a valid address range, addr=0x{:016X}, size=0x{:016X}",
  825. addr, size);
  826. return ERR_INVALID_ADDRESS_STATE;
  827. }
  828. const auto permissions_type = static_cast<MemoryPermission>(permissions);
  829. if (permissions_type != MemoryPermission::Read &&
  830. permissions_type != MemoryPermission::ReadWrite) {
  831. LOG_ERROR(Kernel_SVC, "Expected Read or ReadWrite permission but got permissions=0x{:08X}",
  832. permissions);
  833. return ERR_INVALID_MEMORY_PERMISSIONS;
  834. }
  835. auto* const current_process = Core::CurrentProcess();
  836. auto shared_memory = current_process->GetHandleTable().Get<SharedMemory>(shared_memory_handle);
  837. if (!shared_memory) {
  838. LOG_ERROR(Kernel_SVC, "Shared memory does not exist, shared_memory_handle=0x{:08X}",
  839. shared_memory_handle);
  840. return ERR_INVALID_HANDLE;
  841. }
  842. const auto& vm_manager = current_process->VMManager();
  843. if (!vm_manager.IsWithinASLRRegion(addr, size)) {
  844. LOG_ERROR(Kernel_SVC, "Region is not within the ASLR region. addr=0x{:016X}, size={:016X}",
  845. addr, size);
  846. return ERR_INVALID_MEMORY_RANGE;
  847. }
  848. return shared_memory->Map(*current_process, addr, permissions_type, MemoryPermission::DontCare);
  849. }
  850. static ResultCode UnmapSharedMemory(Handle shared_memory_handle, VAddr addr, u64 size) {
  851. LOG_WARNING(Kernel_SVC, "called, shared_memory_handle=0x{:08X}, addr=0x{:X}, size=0x{:X}",
  852. shared_memory_handle, addr, size);
  853. if (!Common::Is4KBAligned(addr)) {
  854. LOG_ERROR(Kernel_SVC, "Address is not aligned to 4KB, addr=0x{:016X}", addr);
  855. return ERR_INVALID_ADDRESS;
  856. }
  857. if (size == 0) {
  858. LOG_ERROR(Kernel_SVC, "Size is 0");
  859. return ERR_INVALID_SIZE;
  860. }
  861. if (!Common::Is4KBAligned(size)) {
  862. LOG_ERROR(Kernel_SVC, "Size is not aligned to 4KB, size=0x{:016X}", size);
  863. return ERR_INVALID_SIZE;
  864. }
  865. if (!IsValidAddressRange(addr, size)) {
  866. LOG_ERROR(Kernel_SVC, "Region is not a valid address range, addr=0x{:016X}, size=0x{:016X}",
  867. addr, size);
  868. return ERR_INVALID_ADDRESS_STATE;
  869. }
  870. auto* const current_process = Core::CurrentProcess();
  871. auto shared_memory = current_process->GetHandleTable().Get<SharedMemory>(shared_memory_handle);
  872. if (!shared_memory) {
  873. LOG_ERROR(Kernel_SVC, "Shared memory does not exist, shared_memory_handle=0x{:08X}",
  874. shared_memory_handle);
  875. return ERR_INVALID_HANDLE;
  876. }
  877. const auto& vm_manager = current_process->VMManager();
  878. if (!vm_manager.IsWithinASLRRegion(addr, size)) {
  879. LOG_ERROR(Kernel_SVC, "Region is not within the ASLR region. addr=0x{:016X}, size={:016X}",
  880. addr, size);
  881. return ERR_INVALID_MEMORY_RANGE;
  882. }
  883. return shared_memory->Unmap(*current_process, addr);
  884. }
  885. /// Query process memory
  886. static ResultCode QueryProcessMemory(MemoryInfo* memory_info, PageInfo* /*page_info*/,
  887. Handle process_handle, u64 addr) {
  888. LOG_TRACE(Kernel_SVC, "called process=0x{:08X} addr={:X}", process_handle, addr);
  889. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  890. SharedPtr<Process> process = handle_table.Get<Process>(process_handle);
  891. if (!process) {
  892. LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}",
  893. process_handle);
  894. return ERR_INVALID_HANDLE;
  895. }
  896. const auto& vm_manager = process->VMManager();
  897. const auto vma = vm_manager.FindVMA(addr);
  898. memory_info->attributes = 0;
  899. if (vm_manager.IsValidHandle(vma)) {
  900. memory_info->base_address = vma->second.base;
  901. memory_info->permission = static_cast<u32>(vma->second.permissions);
  902. memory_info->size = vma->second.size;
  903. memory_info->type = static_cast<u32>(vma->second.meminfo_state);
  904. } else {
  905. memory_info->base_address = 0;
  906. memory_info->permission = static_cast<u32>(VMAPermission::None);
  907. memory_info->size = 0;
  908. memory_info->type = static_cast<u32>(MemoryState::Unmapped);
  909. }
  910. return RESULT_SUCCESS;
  911. }
  912. /// Query memory
  913. static ResultCode QueryMemory(MemoryInfo* memory_info, PageInfo* page_info, VAddr addr) {
  914. LOG_TRACE(Kernel_SVC, "called, addr={:X}", addr);
  915. return QueryProcessMemory(memory_info, page_info, CurrentProcess, addr);
  916. }
  917. /// Exits the current process
  918. static void ExitProcess() {
  919. auto* current_process = Core::CurrentProcess();
  920. LOG_INFO(Kernel_SVC, "Process {} exiting", current_process->GetProcessID());
  921. ASSERT_MSG(current_process->GetStatus() == ProcessStatus::Running,
  922. "Process has already exited");
  923. current_process->PrepareForTermination();
  924. // Kill the current thread
  925. GetCurrentThread()->Stop();
  926. Core::System::GetInstance().PrepareReschedule();
  927. }
  928. /// Creates a new thread
  929. static ResultCode CreateThread(Handle* out_handle, VAddr entry_point, u64 arg, VAddr stack_top,
  930. u32 priority, s32 processor_id) {
  931. LOG_TRACE(Kernel_SVC,
  932. "called entrypoint=0x{:08X}, arg=0x{:08X}, stacktop=0x{:08X}, "
  933. "threadpriority=0x{:08X}, processorid=0x{:08X} : created handle=0x{:08X}",
  934. entry_point, arg, stack_top, priority, processor_id, *out_handle);
  935. if (priority > THREADPRIO_LOWEST) {
  936. LOG_ERROR(Kernel_SVC, "An invalid priority was specified, expected {} but got {}",
  937. THREADPRIO_LOWEST, priority);
  938. return ERR_INVALID_THREAD_PRIORITY;
  939. }
  940. auto* const current_process = Core::CurrentProcess();
  941. if (processor_id == THREADPROCESSORID_DEFAULT) {
  942. // Set the target CPU to the one specified in the process' exheader.
  943. processor_id = current_process->GetDefaultProcessorID();
  944. ASSERT(processor_id != THREADPROCESSORID_DEFAULT);
  945. }
  946. switch (processor_id) {
  947. case THREADPROCESSORID_0:
  948. case THREADPROCESSORID_1:
  949. case THREADPROCESSORID_2:
  950. case THREADPROCESSORID_3:
  951. break;
  952. default:
  953. LOG_ERROR(Kernel_SVC, "Invalid thread processor ID: {}", processor_id);
  954. return ERR_INVALID_PROCESSOR_ID;
  955. }
  956. const std::string name = fmt::format("thread-{:X}", entry_point);
  957. auto& kernel = Core::System::GetInstance().Kernel();
  958. CASCADE_RESULT(SharedPtr<Thread> thread,
  959. Thread::Create(kernel, name, entry_point, priority, arg, processor_id, stack_top,
  960. *current_process));
  961. const auto new_guest_handle = current_process->GetHandleTable().Create(thread);
  962. if (new_guest_handle.Failed()) {
  963. LOG_ERROR(Kernel_SVC, "Failed to create handle with error=0x{:X}",
  964. new_guest_handle.Code().raw);
  965. return new_guest_handle.Code();
  966. }
  967. thread->SetGuestHandle(*new_guest_handle);
  968. *out_handle = *new_guest_handle;
  969. Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule();
  970. return RESULT_SUCCESS;
  971. }
  972. /// Starts the thread for the provided handle
  973. static ResultCode StartThread(Handle thread_handle) {
  974. LOG_TRACE(Kernel_SVC, "called thread=0x{:08X}", thread_handle);
  975. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  976. const SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  977. if (!thread) {
  978. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, thread_handle=0x{:08X}",
  979. thread_handle);
  980. return ERR_INVALID_HANDLE;
  981. }
  982. ASSERT(thread->GetStatus() == ThreadStatus::Dormant);
  983. thread->ResumeFromWait();
  984. Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule();
  985. return RESULT_SUCCESS;
  986. }
  987. /// Called when a thread exits
  988. static void ExitThread() {
  989. LOG_TRACE(Kernel_SVC, "called, pc=0x{:08X}", Core::CurrentArmInterface().GetPC());
  990. ExitCurrentThread();
  991. Core::System::GetInstance().PrepareReschedule();
  992. }
  993. /// Sleep the current thread
  994. static void SleepThread(s64 nanoseconds) {
  995. LOG_TRACE(Kernel_SVC, "called nanoseconds={}", nanoseconds);
  996. // Don't attempt to yield execution if there are no available threads to run,
  997. // this way we avoid a useless reschedule to the idle thread.
  998. if (nanoseconds == 0 && !Core::System::GetInstance().CurrentScheduler().HaveReadyThreads())
  999. return;
  1000. // Sleep current thread and check for next thread to schedule
  1001. WaitCurrentThread_Sleep();
  1002. // Create an event to wake the thread up after the specified nanosecond delay has passed
  1003. GetCurrentThread()->WakeAfterDelay(nanoseconds);
  1004. Core::System::GetInstance().PrepareReschedule();
  1005. }
  1006. /// Wait process wide key atomic
  1007. static ResultCode WaitProcessWideKeyAtomic(VAddr mutex_addr, VAddr condition_variable_addr,
  1008. Handle thread_handle, s64 nano_seconds) {
  1009. LOG_TRACE(
  1010. Kernel_SVC,
  1011. "called mutex_addr={:X}, condition_variable_addr={:X}, thread_handle=0x{:08X}, timeout={}",
  1012. mutex_addr, condition_variable_addr, thread_handle, nano_seconds);
  1013. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  1014. SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  1015. ASSERT(thread);
  1016. CASCADE_CODE(Mutex::Release(mutex_addr));
  1017. SharedPtr<Thread> current_thread = GetCurrentThread();
  1018. current_thread->SetCondVarWaitAddress(condition_variable_addr);
  1019. current_thread->SetMutexWaitAddress(mutex_addr);
  1020. current_thread->SetWaitHandle(thread_handle);
  1021. current_thread->SetStatus(ThreadStatus::WaitMutex);
  1022. current_thread->InvalidateWakeupCallback();
  1023. current_thread->WakeAfterDelay(nano_seconds);
  1024. // Note: Deliberately don't attempt to inherit the lock owner's priority.
  1025. Core::System::GetInstance().CpuCore(current_thread->GetProcessorID()).PrepareReschedule();
  1026. return RESULT_SUCCESS;
  1027. }
  1028. /// Signal process wide key
  1029. static ResultCode SignalProcessWideKey(VAddr condition_variable_addr, s32 target) {
  1030. LOG_TRACE(Kernel_SVC, "called, condition_variable_addr=0x{:X}, target=0x{:08X}",
  1031. condition_variable_addr, target);
  1032. const auto RetrieveWaitingThreads = [](std::size_t core_index,
  1033. std::vector<SharedPtr<Thread>>& waiting_threads,
  1034. VAddr condvar_addr) {
  1035. const auto& scheduler = Core::System::GetInstance().Scheduler(core_index);
  1036. const auto& thread_list = scheduler.GetThreadList();
  1037. for (const auto& thread : thread_list) {
  1038. if (thread->GetCondVarWaitAddress() == condvar_addr)
  1039. waiting_threads.push_back(thread);
  1040. }
  1041. };
  1042. // Retrieve a list of all threads that are waiting for this condition variable.
  1043. std::vector<SharedPtr<Thread>> waiting_threads;
  1044. RetrieveWaitingThreads(0, waiting_threads, condition_variable_addr);
  1045. RetrieveWaitingThreads(1, waiting_threads, condition_variable_addr);
  1046. RetrieveWaitingThreads(2, waiting_threads, condition_variable_addr);
  1047. RetrieveWaitingThreads(3, waiting_threads, condition_variable_addr);
  1048. // Sort them by priority, such that the highest priority ones come first.
  1049. std::sort(waiting_threads.begin(), waiting_threads.end(),
  1050. [](const SharedPtr<Thread>& lhs, const SharedPtr<Thread>& rhs) {
  1051. return lhs->GetPriority() < rhs->GetPriority();
  1052. });
  1053. // Only process up to 'target' threads, unless 'target' is -1, in which case process
  1054. // them all.
  1055. std::size_t last = waiting_threads.size();
  1056. if (target != -1)
  1057. last = target;
  1058. // If there are no threads waiting on this condition variable, just exit
  1059. if (last > waiting_threads.size())
  1060. return RESULT_SUCCESS;
  1061. for (std::size_t index = 0; index < last; ++index) {
  1062. auto& thread = waiting_threads[index];
  1063. ASSERT(thread->GetCondVarWaitAddress() == condition_variable_addr);
  1064. std::size_t current_core = Core::System::GetInstance().CurrentCoreIndex();
  1065. auto& monitor = Core::System::GetInstance().Monitor();
  1066. // Atomically read the value of the mutex.
  1067. u32 mutex_val = 0;
  1068. do {
  1069. monitor.SetExclusive(current_core, thread->GetMutexWaitAddress());
  1070. // If the mutex is not yet acquired, acquire it.
  1071. mutex_val = Memory::Read32(thread->GetMutexWaitAddress());
  1072. if (mutex_val != 0) {
  1073. monitor.ClearExclusive();
  1074. break;
  1075. }
  1076. } while (!monitor.ExclusiveWrite32(current_core, thread->GetMutexWaitAddress(),
  1077. thread->GetWaitHandle()));
  1078. if (mutex_val == 0) {
  1079. // We were able to acquire the mutex, resume this thread.
  1080. ASSERT(thread->GetStatus() == ThreadStatus::WaitMutex);
  1081. thread->ResumeFromWait();
  1082. auto* const lock_owner = thread->GetLockOwner();
  1083. if (lock_owner != nullptr) {
  1084. lock_owner->RemoveMutexWaiter(thread);
  1085. }
  1086. thread->SetLockOwner(nullptr);
  1087. thread->SetMutexWaitAddress(0);
  1088. thread->SetCondVarWaitAddress(0);
  1089. thread->SetWaitHandle(0);
  1090. } else {
  1091. // Atomically signal that the mutex now has a waiting thread.
  1092. do {
  1093. monitor.SetExclusive(current_core, thread->GetMutexWaitAddress());
  1094. // Ensure that the mutex value is still what we expect.
  1095. u32 value = Memory::Read32(thread->GetMutexWaitAddress());
  1096. // TODO(Subv): When this happens, the kernel just clears the exclusive state and
  1097. // retries the initial read for this thread.
  1098. ASSERT_MSG(mutex_val == value, "Unhandled synchronization primitive case");
  1099. } while (!monitor.ExclusiveWrite32(current_core, thread->GetMutexWaitAddress(),
  1100. mutex_val | Mutex::MutexHasWaitersFlag));
  1101. // The mutex is already owned by some other thread, make this thread wait on it.
  1102. const Handle owner_handle = static_cast<Handle>(mutex_val & Mutex::MutexOwnerMask);
  1103. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  1104. auto owner = handle_table.Get<Thread>(owner_handle);
  1105. ASSERT(owner);
  1106. ASSERT(thread->GetStatus() == ThreadStatus::WaitMutex);
  1107. thread->InvalidateWakeupCallback();
  1108. owner->AddMutexWaiter(thread);
  1109. Core::System::GetInstance().CpuCore(thread->GetProcessorID()).PrepareReschedule();
  1110. }
  1111. }
  1112. return RESULT_SUCCESS;
  1113. }
  1114. // Wait for an address (via Address Arbiter)
  1115. static ResultCode WaitForAddress(VAddr address, u32 type, s32 value, s64 timeout) {
  1116. LOG_WARNING(Kernel_SVC, "called, address=0x{:X}, type=0x{:X}, value=0x{:X}, timeout={}",
  1117. address, type, value, timeout);
  1118. // If the passed address is a kernel virtual address, return invalid memory state.
  1119. if (Memory::IsKernelVirtualAddress(address)) {
  1120. LOG_ERROR(Kernel_SVC, "Address is a kernel virtual address, address={:016X}", address);
  1121. return ERR_INVALID_ADDRESS_STATE;
  1122. }
  1123. // If the address is not properly aligned to 4 bytes, return invalid address.
  1124. if (!Common::IsWordAligned(address)) {
  1125. LOG_ERROR(Kernel_SVC, "Address is not word aligned, address={:016X}", address);
  1126. return ERR_INVALID_ADDRESS;
  1127. }
  1128. switch (static_cast<AddressArbiter::ArbitrationType>(type)) {
  1129. case AddressArbiter::ArbitrationType::WaitIfLessThan:
  1130. return AddressArbiter::WaitForAddressIfLessThan(address, value, timeout, false);
  1131. case AddressArbiter::ArbitrationType::DecrementAndWaitIfLessThan:
  1132. return AddressArbiter::WaitForAddressIfLessThan(address, value, timeout, true);
  1133. case AddressArbiter::ArbitrationType::WaitIfEqual:
  1134. return AddressArbiter::WaitForAddressIfEqual(address, value, timeout);
  1135. default:
  1136. LOG_ERROR(Kernel_SVC,
  1137. "Invalid arbitration type, expected WaitIfLessThan, DecrementAndWaitIfLessThan "
  1138. "or WaitIfEqual but got {}",
  1139. type);
  1140. return ERR_INVALID_ENUM_VALUE;
  1141. }
  1142. }
  1143. // Signals to an address (via Address Arbiter)
  1144. static ResultCode SignalToAddress(VAddr address, u32 type, s32 value, s32 num_to_wake) {
  1145. LOG_WARNING(Kernel_SVC, "called, address=0x{:X}, type=0x{:X}, value=0x{:X}, num_to_wake=0x{:X}",
  1146. address, type, value, num_to_wake);
  1147. // If the passed address is a kernel virtual address, return invalid memory state.
  1148. if (Memory::IsKernelVirtualAddress(address)) {
  1149. LOG_ERROR(Kernel_SVC, "Address is a kernel virtual address, address={:016X}", address);
  1150. return ERR_INVALID_ADDRESS_STATE;
  1151. }
  1152. // If the address is not properly aligned to 4 bytes, return invalid address.
  1153. if (!Common::IsWordAligned(address)) {
  1154. LOG_ERROR(Kernel_SVC, "Address is not word aligned, address={:016X}", address);
  1155. return ERR_INVALID_ADDRESS;
  1156. }
  1157. switch (static_cast<AddressArbiter::SignalType>(type)) {
  1158. case AddressArbiter::SignalType::Signal:
  1159. return AddressArbiter::SignalToAddress(address, num_to_wake);
  1160. case AddressArbiter::SignalType::IncrementAndSignalIfEqual:
  1161. return AddressArbiter::IncrementAndSignalToAddressIfEqual(address, value, num_to_wake);
  1162. case AddressArbiter::SignalType::ModifyByWaitingCountAndSignalIfEqual:
  1163. return AddressArbiter::ModifyByWaitingCountAndSignalToAddressIfEqual(address, value,
  1164. num_to_wake);
  1165. default:
  1166. LOG_ERROR(Kernel_SVC,
  1167. "Invalid signal type, expected Signal, IncrementAndSignalIfEqual "
  1168. "or ModifyByWaitingCountAndSignalIfEqual but got {}",
  1169. type);
  1170. return ERR_INVALID_ENUM_VALUE;
  1171. }
  1172. }
  1173. /// This returns the total CPU ticks elapsed since the CPU was powered-on
  1174. static u64 GetSystemTick() {
  1175. LOG_TRACE(Kernel_SVC, "called");
  1176. const u64 result{CoreTiming::GetTicks()};
  1177. // Advance time to defeat dumb games that busy-wait for the frame to end.
  1178. CoreTiming::AddTicks(400);
  1179. return result;
  1180. }
  1181. /// Close a handle
  1182. static ResultCode CloseHandle(Handle handle) {
  1183. LOG_TRACE(Kernel_SVC, "Closing handle 0x{:08X}", handle);
  1184. auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  1185. return handle_table.Close(handle);
  1186. }
  1187. /// Clears the signaled state of an event or process.
  1188. static ResultCode ResetSignal(Handle handle) {
  1189. LOG_DEBUG(Kernel_SVC, "called handle 0x{:08X}", handle);
  1190. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  1191. auto event = handle_table.Get<ReadableEvent>(handle);
  1192. if (event) {
  1193. return event->Reset();
  1194. }
  1195. auto process = handle_table.Get<Process>(handle);
  1196. if (process) {
  1197. return process->ClearSignalState();
  1198. }
  1199. LOG_ERROR(Kernel_SVC, "Invalid handle (0x{:08X})", handle);
  1200. return ERR_INVALID_HANDLE;
  1201. }
  1202. /// Creates a TransferMemory object
  1203. static ResultCode CreateTransferMemory(Handle* handle, VAddr addr, u64 size, u32 permissions) {
  1204. LOG_DEBUG(Kernel_SVC, "called addr=0x{:X}, size=0x{:X}, perms=0x{:08X}", addr, size,
  1205. permissions);
  1206. if (!Common::Is4KBAligned(addr)) {
  1207. LOG_ERROR(Kernel_SVC, "Address ({:016X}) is not page aligned!", addr);
  1208. return ERR_INVALID_ADDRESS;
  1209. }
  1210. if (!Common::Is4KBAligned(size) || size == 0) {
  1211. LOG_ERROR(Kernel_SVC, "Size ({:016X}) is not page aligned or equal to zero!", size);
  1212. return ERR_INVALID_ADDRESS;
  1213. }
  1214. if (!IsValidAddressRange(addr, size)) {
  1215. LOG_ERROR(Kernel_SVC, "Address and size cause overflow! (address={:016X}, size={:016X})",
  1216. addr, size);
  1217. return ERR_INVALID_ADDRESS_STATE;
  1218. }
  1219. const auto perms = static_cast<MemoryPermission>(permissions);
  1220. if (perms != MemoryPermission::None && perms != MemoryPermission::Read &&
  1221. perms != MemoryPermission::ReadWrite) {
  1222. LOG_ERROR(Kernel_SVC, "Invalid memory permissions for transfer memory! (perms={:08X})",
  1223. permissions);
  1224. return ERR_INVALID_MEMORY_PERMISSIONS;
  1225. }
  1226. auto& kernel = Core::System::GetInstance().Kernel();
  1227. auto process = kernel.CurrentProcess();
  1228. auto& handle_table = process->GetHandleTable();
  1229. const auto shared_mem_handle = SharedMemory::Create(kernel, process, size, perms, perms, addr);
  1230. CASCADE_RESULT(*handle, handle_table.Create(shared_mem_handle));
  1231. return RESULT_SUCCESS;
  1232. }
  1233. static ResultCode GetThreadCoreMask(Handle thread_handle, u32* core, u64* mask) {
  1234. LOG_TRACE(Kernel_SVC, "called, handle=0x{:08X}", thread_handle);
  1235. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  1236. const SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  1237. if (!thread) {
  1238. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, thread_handle=0x{:08X}",
  1239. thread_handle);
  1240. return ERR_INVALID_HANDLE;
  1241. }
  1242. *core = thread->GetIdealCore();
  1243. *mask = thread->GetAffinityMask();
  1244. return RESULT_SUCCESS;
  1245. }
  1246. static ResultCode SetThreadCoreMask(Handle thread_handle, u32 core, u64 mask) {
  1247. LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, mask=0x{:016X}, core=0x{:X}", thread_handle,
  1248. mask, core);
  1249. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  1250. const SharedPtr<Thread> thread = handle_table.Get<Thread>(thread_handle);
  1251. if (!thread) {
  1252. LOG_ERROR(Kernel_SVC, "Thread handle does not exist, thread_handle=0x{:08X}",
  1253. thread_handle);
  1254. return ERR_INVALID_HANDLE;
  1255. }
  1256. if (core == static_cast<u32>(THREADPROCESSORID_DEFAULT)) {
  1257. const u8 default_processor_id = thread->GetOwnerProcess()->GetDefaultProcessorID();
  1258. ASSERT(default_processor_id != static_cast<u8>(THREADPROCESSORID_DEFAULT));
  1259. // Set the target CPU to the one specified in the process' exheader.
  1260. core = default_processor_id;
  1261. mask = 1ULL << core;
  1262. }
  1263. if (mask == 0) {
  1264. LOG_ERROR(Kernel_SVC, "Mask is 0");
  1265. return ERR_INVALID_COMBINATION;
  1266. }
  1267. /// This value is used to only change the affinity mask without changing the current ideal core.
  1268. static constexpr u32 OnlyChangeMask = static_cast<u32>(-3);
  1269. if (core == OnlyChangeMask) {
  1270. core = thread->GetIdealCore();
  1271. } else if (core >= Core::NUM_CPU_CORES && core != static_cast<u32>(-1)) {
  1272. LOG_ERROR(Kernel_SVC, "Invalid core specified, got {}", core);
  1273. return ERR_INVALID_PROCESSOR_ID;
  1274. }
  1275. // Error out if the input core isn't enabled in the input mask.
  1276. if (core < Core::NUM_CPU_CORES && (mask & (1ull << core)) == 0) {
  1277. LOG_ERROR(Kernel_SVC, "Core is not enabled for the current mask, core={}, mask={:016X}",
  1278. core, mask);
  1279. return ERR_INVALID_COMBINATION;
  1280. }
  1281. thread->ChangeCore(core, mask);
  1282. return RESULT_SUCCESS;
  1283. }
  1284. static ResultCode CreateSharedMemory(Handle* handle, u64 size, u32 local_permissions,
  1285. u32 remote_permissions) {
  1286. LOG_TRACE(Kernel_SVC, "called, size=0x{:X}, localPerms=0x{:08X}, remotePerms=0x{:08X}", size,
  1287. local_permissions, remote_permissions);
  1288. if (size == 0) {
  1289. LOG_ERROR(Kernel_SVC, "Size is 0");
  1290. return ERR_INVALID_SIZE;
  1291. }
  1292. if (!Common::Is4KBAligned(size)) {
  1293. LOG_ERROR(Kernel_SVC, "Size is not aligned to 4KB, 0x{:016X}", size);
  1294. return ERR_INVALID_SIZE;
  1295. }
  1296. if (size >= MAIN_MEMORY_SIZE) {
  1297. LOG_ERROR(Kernel_SVC, "Size is not less than 8GB, 0x{:016X}", size);
  1298. return ERR_INVALID_SIZE;
  1299. }
  1300. const auto local_perms = static_cast<MemoryPermission>(local_permissions);
  1301. if (local_perms != MemoryPermission::Read && local_perms != MemoryPermission::ReadWrite) {
  1302. LOG_ERROR(Kernel_SVC,
  1303. "Invalid local memory permissions, expected Read or ReadWrite but got "
  1304. "local_permissions={}",
  1305. static_cast<u32>(local_permissions));
  1306. return ERR_INVALID_MEMORY_PERMISSIONS;
  1307. }
  1308. const auto remote_perms = static_cast<MemoryPermission>(remote_permissions);
  1309. if (remote_perms != MemoryPermission::Read && remote_perms != MemoryPermission::ReadWrite &&
  1310. remote_perms != MemoryPermission::DontCare) {
  1311. LOG_ERROR(Kernel_SVC,
  1312. "Invalid remote memory permissions, expected Read, ReadWrite or DontCare but got "
  1313. "remote_permissions={}",
  1314. static_cast<u32>(remote_permissions));
  1315. return ERR_INVALID_MEMORY_PERMISSIONS;
  1316. }
  1317. auto& kernel = Core::System::GetInstance().Kernel();
  1318. auto process = kernel.CurrentProcess();
  1319. auto& handle_table = process->GetHandleTable();
  1320. auto shared_mem_handle = SharedMemory::Create(kernel, process, size, local_perms, remote_perms);
  1321. CASCADE_RESULT(*handle, handle_table.Create(shared_mem_handle));
  1322. return RESULT_SUCCESS;
  1323. }
  1324. static ResultCode CreateEvent(Handle* write_handle, Handle* read_handle) {
  1325. LOG_DEBUG(Kernel_SVC, "called");
  1326. auto& kernel = Core::System::GetInstance().Kernel();
  1327. const auto [readable_event, writable_event] =
  1328. WritableEvent::CreateEventPair(kernel, ResetType::Sticky, "CreateEvent");
  1329. HandleTable& handle_table = kernel.CurrentProcess()->GetHandleTable();
  1330. const auto write_create_result = handle_table.Create(writable_event);
  1331. if (write_create_result.Failed()) {
  1332. return write_create_result.Code();
  1333. }
  1334. *write_handle = *write_create_result;
  1335. const auto read_create_result = handle_table.Create(readable_event);
  1336. if (read_create_result.Failed()) {
  1337. handle_table.Close(*write_create_result);
  1338. return read_create_result.Code();
  1339. }
  1340. *read_handle = *read_create_result;
  1341. LOG_DEBUG(Kernel_SVC,
  1342. "successful. Writable event handle=0x{:08X}, Readable event handle=0x{:08X}",
  1343. *write_create_result, *read_create_result);
  1344. return RESULT_SUCCESS;
  1345. }
  1346. static ResultCode ClearEvent(Handle handle) {
  1347. LOG_TRACE(Kernel_SVC, "called, event=0x{:08X}", handle);
  1348. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  1349. auto writable_event = handle_table.Get<WritableEvent>(handle);
  1350. if (writable_event) {
  1351. writable_event->Clear();
  1352. return RESULT_SUCCESS;
  1353. }
  1354. auto readable_event = handle_table.Get<ReadableEvent>(handle);
  1355. if (readable_event) {
  1356. readable_event->Clear();
  1357. return RESULT_SUCCESS;
  1358. }
  1359. LOG_ERROR(Kernel_SVC, "Event handle does not exist, handle=0x{:08X}", handle);
  1360. return ERR_INVALID_HANDLE;
  1361. }
  1362. static ResultCode SignalEvent(Handle handle) {
  1363. LOG_DEBUG(Kernel_SVC, "called. Handle=0x{:08X}", handle);
  1364. HandleTable& handle_table = Core::CurrentProcess()->GetHandleTable();
  1365. auto writable_event = handle_table.Get<WritableEvent>(handle);
  1366. if (!writable_event) {
  1367. LOG_ERROR(Kernel_SVC, "Non-existent writable event handle used (0x{:08X})", handle);
  1368. return ERR_INVALID_HANDLE;
  1369. }
  1370. writable_event->Signal();
  1371. return RESULT_SUCCESS;
  1372. }
  1373. static ResultCode GetProcessInfo(u64* out, Handle process_handle, u32 type) {
  1374. LOG_DEBUG(Kernel_SVC, "called, handle=0x{:08X}, type=0x{:X}", process_handle, type);
  1375. // This function currently only allows retrieving a process' status.
  1376. enum class InfoType {
  1377. Status,
  1378. };
  1379. const auto& handle_table = Core::CurrentProcess()->GetHandleTable();
  1380. const auto process = handle_table.Get<Process>(process_handle);
  1381. if (!process) {
  1382. LOG_ERROR(Kernel_SVC, "Process handle does not exist, process_handle=0x{:08X}",
  1383. process_handle);
  1384. return ERR_INVALID_HANDLE;
  1385. }
  1386. const auto info_type = static_cast<InfoType>(type);
  1387. if (info_type != InfoType::Status) {
  1388. LOG_ERROR(Kernel_SVC, "Expected info_type to be Status but got {} instead", type);
  1389. return ERR_INVALID_ENUM_VALUE;
  1390. }
  1391. *out = static_cast<u64>(process->GetStatus());
  1392. return RESULT_SUCCESS;
  1393. }
  1394. static ResultCode CreateResourceLimit(Handle* out_handle) {
  1395. LOG_DEBUG(Kernel_SVC, "called");
  1396. auto& kernel = Core::System::GetInstance().Kernel();
  1397. auto resource_limit = ResourceLimit::Create(kernel);
  1398. auto* const current_process = kernel.CurrentProcess();
  1399. ASSERT(current_process != nullptr);
  1400. const auto handle = current_process->GetHandleTable().Create(std::move(resource_limit));
  1401. if (handle.Failed()) {
  1402. return handle.Code();
  1403. }
  1404. *out_handle = *handle;
  1405. return RESULT_SUCCESS;
  1406. }
  1407. static ResultCode GetResourceLimitLimitValue(u64* out_value, Handle resource_limit,
  1408. u32 resource_type) {
  1409. LOG_DEBUG(Kernel_SVC, "called. Handle={:08X}, Resource type={}", resource_limit, resource_type);
  1410. const auto limit_value = RetrieveResourceLimitValue(resource_limit, resource_type,
  1411. ResourceLimitValueType::LimitValue);
  1412. if (limit_value.Failed()) {
  1413. return limit_value.Code();
  1414. }
  1415. *out_value = static_cast<u64>(*limit_value);
  1416. return RESULT_SUCCESS;
  1417. }
  1418. static ResultCode GetResourceLimitCurrentValue(u64* out_value, Handle resource_limit,
  1419. u32 resource_type) {
  1420. LOG_DEBUG(Kernel_SVC, "called. Handle={:08X}, Resource type={}", resource_limit, resource_type);
  1421. const auto current_value = RetrieveResourceLimitValue(resource_limit, resource_type,
  1422. ResourceLimitValueType::CurrentValue);
  1423. if (current_value.Failed()) {
  1424. return current_value.Code();
  1425. }
  1426. *out_value = static_cast<u64>(*current_value);
  1427. return RESULT_SUCCESS;
  1428. }
  1429. static ResultCode SetResourceLimitLimitValue(Handle resource_limit, u32 resource_type, u64 value) {
  1430. LOG_DEBUG(Kernel_SVC, "called. Handle={:08X}, Resource type={}, Value={}", resource_limit,
  1431. resource_type, value);
  1432. const auto type = static_cast<ResourceType>(resource_type);
  1433. if (!IsValidResourceType(type)) {
  1434. LOG_ERROR(Kernel_SVC, "Invalid resource limit type: '{}'", resource_type);
  1435. return ERR_INVALID_ENUM_VALUE;
  1436. }
  1437. auto& kernel = Core::System::GetInstance().Kernel();
  1438. auto* const current_process = kernel.CurrentProcess();
  1439. ASSERT(current_process != nullptr);
  1440. auto resource_limit_object =
  1441. current_process->GetHandleTable().Get<ResourceLimit>(resource_limit);
  1442. if (!resource_limit_object) {
  1443. LOG_ERROR(Kernel_SVC, "Handle to non-existent resource limit instance used. Handle={:08X}",
  1444. resource_limit);
  1445. return ERR_INVALID_HANDLE;
  1446. }
  1447. const auto set_result = resource_limit_object->SetLimitValue(type, static_cast<s64>(value));
  1448. if (set_result.IsError()) {
  1449. LOG_ERROR(
  1450. Kernel_SVC,
  1451. "Attempted to lower resource limit ({}) for category '{}' below its current value ({})",
  1452. resource_limit_object->GetMaxResourceValue(type), resource_type,
  1453. resource_limit_object->GetCurrentResourceValue(type));
  1454. return set_result;
  1455. }
  1456. return RESULT_SUCCESS;
  1457. }
  1458. namespace {
  1459. struct FunctionDef {
  1460. using Func = void();
  1461. u32 id;
  1462. Func* func;
  1463. const char* name;
  1464. };
  1465. } // namespace
  1466. static const FunctionDef SVC_Table[] = {
  1467. {0x00, nullptr, "Unknown"},
  1468. {0x01, SvcWrap<SetHeapSize>, "SetHeapSize"},
  1469. {0x02, SvcWrap<SetMemoryPermission>, "SetMemoryPermission"},
  1470. {0x03, SvcWrap<SetMemoryAttribute>, "SetMemoryAttribute"},
  1471. {0x04, SvcWrap<MapMemory>, "MapMemory"},
  1472. {0x05, SvcWrap<UnmapMemory>, "UnmapMemory"},
  1473. {0x06, SvcWrap<QueryMemory>, "QueryMemory"},
  1474. {0x07, SvcWrap<ExitProcess>, "ExitProcess"},
  1475. {0x08, SvcWrap<CreateThread>, "CreateThread"},
  1476. {0x09, SvcWrap<StartThread>, "StartThread"},
  1477. {0x0A, SvcWrap<ExitThread>, "ExitThread"},
  1478. {0x0B, SvcWrap<SleepThread>, "SleepThread"},
  1479. {0x0C, SvcWrap<GetThreadPriority>, "GetThreadPriority"},
  1480. {0x0D, SvcWrap<SetThreadPriority>, "SetThreadPriority"},
  1481. {0x0E, SvcWrap<GetThreadCoreMask>, "GetThreadCoreMask"},
  1482. {0x0F, SvcWrap<SetThreadCoreMask>, "SetThreadCoreMask"},
  1483. {0x10, SvcWrap<GetCurrentProcessorNumber>, "GetCurrentProcessorNumber"},
  1484. {0x11, SvcWrap<SignalEvent>, "SignalEvent"},
  1485. {0x12, SvcWrap<ClearEvent>, "ClearEvent"},
  1486. {0x13, SvcWrap<MapSharedMemory>, "MapSharedMemory"},
  1487. {0x14, SvcWrap<UnmapSharedMemory>, "UnmapSharedMemory"},
  1488. {0x15, SvcWrap<CreateTransferMemory>, "CreateTransferMemory"},
  1489. {0x16, SvcWrap<CloseHandle>, "CloseHandle"},
  1490. {0x17, SvcWrap<ResetSignal>, "ResetSignal"},
  1491. {0x18, SvcWrap<WaitSynchronization>, "WaitSynchronization"},
  1492. {0x19, SvcWrap<CancelSynchronization>, "CancelSynchronization"},
  1493. {0x1A, SvcWrap<ArbitrateLock>, "ArbitrateLock"},
  1494. {0x1B, SvcWrap<ArbitrateUnlock>, "ArbitrateUnlock"},
  1495. {0x1C, SvcWrap<WaitProcessWideKeyAtomic>, "WaitProcessWideKeyAtomic"},
  1496. {0x1D, SvcWrap<SignalProcessWideKey>, "SignalProcessWideKey"},
  1497. {0x1E, SvcWrap<GetSystemTick>, "GetSystemTick"},
  1498. {0x1F, SvcWrap<ConnectToNamedPort>, "ConnectToNamedPort"},
  1499. {0x20, nullptr, "SendSyncRequestLight"},
  1500. {0x21, SvcWrap<SendSyncRequest>, "SendSyncRequest"},
  1501. {0x22, nullptr, "SendSyncRequestWithUserBuffer"},
  1502. {0x23, nullptr, "SendAsyncRequestWithUserBuffer"},
  1503. {0x24, SvcWrap<GetProcessId>, "GetProcessId"},
  1504. {0x25, SvcWrap<GetThreadId>, "GetThreadId"},
  1505. {0x26, SvcWrap<Break>, "Break"},
  1506. {0x27, SvcWrap<OutputDebugString>, "OutputDebugString"},
  1507. {0x28, nullptr, "ReturnFromException"},
  1508. {0x29, SvcWrap<GetInfo>, "GetInfo"},
  1509. {0x2A, nullptr, "FlushEntireDataCache"},
  1510. {0x2B, nullptr, "FlushDataCache"},
  1511. {0x2C, nullptr, "MapPhysicalMemory"},
  1512. {0x2D, nullptr, "UnmapPhysicalMemory"},
  1513. {0x2E, nullptr, "GetFutureThreadInfo"},
  1514. {0x2F, nullptr, "GetLastThreadInfo"},
  1515. {0x30, SvcWrap<GetResourceLimitLimitValue>, "GetResourceLimitLimitValue"},
  1516. {0x31, SvcWrap<GetResourceLimitCurrentValue>, "GetResourceLimitCurrentValue"},
  1517. {0x32, SvcWrap<SetThreadActivity>, "SetThreadActivity"},
  1518. {0x33, SvcWrap<GetThreadContext>, "GetThreadContext"},
  1519. {0x34, SvcWrap<WaitForAddress>, "WaitForAddress"},
  1520. {0x35, SvcWrap<SignalToAddress>, "SignalToAddress"},
  1521. {0x36, nullptr, "Unknown"},
  1522. {0x37, nullptr, "Unknown"},
  1523. {0x38, nullptr, "Unknown"},
  1524. {0x39, nullptr, "Unknown"},
  1525. {0x3A, nullptr, "Unknown"},
  1526. {0x3B, nullptr, "Unknown"},
  1527. {0x3C, nullptr, "DumpInfo"},
  1528. {0x3D, nullptr, "DumpInfoNew"},
  1529. {0x3E, nullptr, "Unknown"},
  1530. {0x3F, nullptr, "Unknown"},
  1531. {0x40, nullptr, "CreateSession"},
  1532. {0x41, nullptr, "AcceptSession"},
  1533. {0x42, nullptr, "ReplyAndReceiveLight"},
  1534. {0x43, nullptr, "ReplyAndReceive"},
  1535. {0x44, nullptr, "ReplyAndReceiveWithUserBuffer"},
  1536. {0x45, SvcWrap<CreateEvent>, "CreateEvent"},
  1537. {0x46, nullptr, "Unknown"},
  1538. {0x47, nullptr, "Unknown"},
  1539. {0x48, nullptr, "MapPhysicalMemoryUnsafe"},
  1540. {0x49, nullptr, "UnmapPhysicalMemoryUnsafe"},
  1541. {0x4A, nullptr, "SetUnsafeLimit"},
  1542. {0x4B, nullptr, "CreateCodeMemory"},
  1543. {0x4C, nullptr, "ControlCodeMemory"},
  1544. {0x4D, nullptr, "SleepSystem"},
  1545. {0x4E, nullptr, "ReadWriteRegister"},
  1546. {0x4F, nullptr, "SetProcessActivity"},
  1547. {0x50, SvcWrap<CreateSharedMemory>, "CreateSharedMemory"},
  1548. {0x51, nullptr, "MapTransferMemory"},
  1549. {0x52, nullptr, "UnmapTransferMemory"},
  1550. {0x53, nullptr, "CreateInterruptEvent"},
  1551. {0x54, nullptr, "QueryPhysicalAddress"},
  1552. {0x55, nullptr, "QueryIoMapping"},
  1553. {0x56, nullptr, "CreateDeviceAddressSpace"},
  1554. {0x57, nullptr, "AttachDeviceAddressSpace"},
  1555. {0x58, nullptr, "DetachDeviceAddressSpace"},
  1556. {0x59, nullptr, "MapDeviceAddressSpaceByForce"},
  1557. {0x5A, nullptr, "MapDeviceAddressSpaceAligned"},
  1558. {0x5B, nullptr, "MapDeviceAddressSpace"},
  1559. {0x5C, nullptr, "UnmapDeviceAddressSpace"},
  1560. {0x5D, nullptr, "InvalidateProcessDataCache"},
  1561. {0x5E, nullptr, "StoreProcessDataCache"},
  1562. {0x5F, nullptr, "FlushProcessDataCache"},
  1563. {0x60, nullptr, "DebugActiveProcess"},
  1564. {0x61, nullptr, "BreakDebugProcess"},
  1565. {0x62, nullptr, "TerminateDebugProcess"},
  1566. {0x63, nullptr, "GetDebugEvent"},
  1567. {0x64, nullptr, "ContinueDebugEvent"},
  1568. {0x65, nullptr, "GetProcessList"},
  1569. {0x66, nullptr, "GetThreadList"},
  1570. {0x67, nullptr, "GetDebugThreadContext"},
  1571. {0x68, nullptr, "SetDebugThreadContext"},
  1572. {0x69, nullptr, "QueryDebugProcessMemory"},
  1573. {0x6A, nullptr, "ReadDebugProcessMemory"},
  1574. {0x6B, nullptr, "WriteDebugProcessMemory"},
  1575. {0x6C, nullptr, "SetHardwareBreakPoint"},
  1576. {0x6D, nullptr, "GetDebugThreadParam"},
  1577. {0x6E, nullptr, "Unknown"},
  1578. {0x6F, nullptr, "GetSystemInfo"},
  1579. {0x70, nullptr, "CreatePort"},
  1580. {0x71, nullptr, "ManageNamedPort"},
  1581. {0x72, nullptr, "ConnectToPort"},
  1582. {0x73, nullptr, "SetProcessMemoryPermission"},
  1583. {0x74, nullptr, "MapProcessMemory"},
  1584. {0x75, nullptr, "UnmapProcessMemory"},
  1585. {0x76, nullptr, "QueryProcessMemory"},
  1586. {0x77, nullptr, "MapProcessCodeMemory"},
  1587. {0x78, nullptr, "UnmapProcessCodeMemory"},
  1588. {0x79, nullptr, "CreateProcess"},
  1589. {0x7A, nullptr, "StartProcess"},
  1590. {0x7B, nullptr, "TerminateProcess"},
  1591. {0x7C, SvcWrap<GetProcessInfo>, "GetProcessInfo"},
  1592. {0x7D, SvcWrap<CreateResourceLimit>, "CreateResourceLimit"},
  1593. {0x7E, SvcWrap<SetResourceLimitLimitValue>, "SetResourceLimitLimitValue"},
  1594. {0x7F, nullptr, "CallSecureMonitor"},
  1595. };
  1596. static const FunctionDef* GetSVCInfo(u32 func_num) {
  1597. if (func_num >= std::size(SVC_Table)) {
  1598. LOG_ERROR(Kernel_SVC, "Unknown svc=0x{:02X}", func_num);
  1599. return nullptr;
  1600. }
  1601. return &SVC_Table[func_num];
  1602. }
  1603. MICROPROFILE_DEFINE(Kernel_SVC, "Kernel", "SVC", MP_RGB(70, 200, 70));
  1604. void CallSVC(u32 immediate) {
  1605. MICROPROFILE_SCOPE(Kernel_SVC);
  1606. // Lock the global kernel mutex when we enter the kernel HLE.
  1607. std::lock_guard<std::recursive_mutex> lock(HLE::g_hle_lock);
  1608. const FunctionDef* info = GetSVCInfo(immediate);
  1609. if (info) {
  1610. if (info->func) {
  1611. info->func();
  1612. } else {
  1613. LOG_CRITICAL(Kernel_SVC, "Unimplemented SVC function {}(..)", info->name);
  1614. }
  1615. } else {
  1616. LOG_CRITICAL(Kernel_SVC, "Unknown SVC function 0x{:X}", immediate);
  1617. }
  1618. }
  1619. } // namespace Kernel