svc.cpp 74 KB

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