svc.cpp 92 KB

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